Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented LineHeight on Label #538

Merged
merged 2 commits into from
Mar 17, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 0 additions & 26 deletions src/Core/src/Platform/iOS/LabelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,32 +79,6 @@ public static void UpdatePadding(this MauiLabel nativeLabel, ILabel label)
(float)label.Padding.Right);
}

public static void UpdateTextDecorations(this UILabel nativeLabel, ILabel label)
{
if (nativeLabel.AttributedText != null && !(nativeLabel.AttributedText?.Length > 0))
return;

var textDecorations = label?.TextDecorations;

var newAttributedText = nativeLabel.AttributedText != null ? new NSMutableAttributedString(nativeLabel.AttributedText) : new NSMutableAttributedString(label?.Text ?? string.Empty);
var strikeThroughStyleKey = UIStringAttributeKey.StrikethroughStyle;
var underlineStyleKey = UIStringAttributeKey.UnderlineStyle;

var range = new NSRange(0, newAttributedText.Length);

if ((textDecorations & TextDecorations.Strikethrough) == 0)
newAttributedText.RemoveAttribute(strikeThroughStyleKey, range);
else
newAttributedText.AddAttribute(strikeThroughStyleKey, NSNumber.FromInt32((int)NSUnderlineStyle.Single), range);

if ((textDecorations & TextDecorations.Underline) == 0)
newAttributedText.RemoveAttribute(underlineStyleKey, range);
else
newAttributedText.AddAttribute(underlineStyleKey, NSNumber.FromInt32((int)NSUnderlineStyle.Single), range);

nativeLabel.AttributedText = newAttributedText;
}

internal static void SetLineBreakMode(this UILabel nativeLabel, ILabel label)
{
int maxLines = label.MaxLines;
Expand Down
5 changes: 3 additions & 2 deletions src/Core/src/Platform/iOS/NSAttributedStringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public static class NSAttributedStringExtensions
if (lineHeight == -1 && attribute == null)
return null;

var mutableParagraphStyle = new NSMutableParagraphStyle(attribute);
mutableParagraphStyle.LineHeightMultiple = new nfloat(lineHeight >= 0 ? lineHeight : -1);
var mutableParagraphStyle = new NSMutableParagraphStyle();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hartez Sorry about the mistake here 😅. I was working blind in VS with no IntelliSense. You can add mutableParagraphStyle.SetParagraphStyle(attribute) right after this line so it copies whatever paragraph style was already there instead of overriding it with the default.

mutableParagraphStyle.LineHeightMultiple = new System.nfloat(lineHeight >= 0 ? lineHeight : -1);

var mutableAttributedString = new NSMutableAttributedString(attributedString);
mutableAttributedString.AddAttribute
Expand All @@ -47,6 +47,7 @@ public static class NSAttributedStringExtensions
mutableParagraphStyle ,
new NSRange(0, mutableAttributedString.Length)
);

return mutableAttributedString;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Threading.Tasks;
using Foundation;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -125,6 +126,7 @@ public async Task LineHeightInitializesCorrectly()

var labelHandler = new LabelStub()
{
Text = "test",
LineHeight = xplatLineHeight
};

Expand Down