I have the below code, that runs in viewDidLoad.
let text = "\n \t What is a Call?"
let attributedText = NSMutableAttributedString(string: text, attributes: [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 30)])
textLabel.attributedText = attributedText
When it runs, nothing gets set in the label. However, if I set it like this
textLabel.text = text
I see the attributed text? Why doesn't the previous line of code work?
I tried adding your code to a completely clean project and found two potential issues. Maybe you have thought of both of them but I couldn't read that from your question so here goes :)
Number of Lines
It is a bit tricky but when you add a \n in your text, that also means that if you haven't explicitly told your UILabel to take that into consideration, it will only show the first line...which is empty in your case :)
So, try adding
textLabel.numberOfLines = 0
to tell your UILabel to use as many lines as is needed for the text.
Size of Your Label
I don't know how you've set up your label, but it could be that the size reserved for the label is too small for all of it to be shown on the screen. You can use the visual debugger to examine the view hierarchy and see if that can give you any clues.
If I set numberOfLines to 0 and added constraints to the textLabel so it had sufficient space, I was able to see "What is a Call?" in glorious 30 point bold system font on my view so...you're close :)
Related
For the first time, I am working with attributed strings so please bear with me!
I have spent a lot of time on SO looking at various attributed string answers but I'm still not convinced I am doing the right thing and would appreciate any advice.
I have created an NSMutableAttributed String (var attributedText) and a simple UIView with a TextView and 2 buttons which should in theory turn any part of highlighted text in the UITextView to bold or italic.
In ViewDidLoad, I have simply added some dummy text and added a couple of attributes to the text such as line spacing and font color. When the view appears all looks as expected.
If I highlight a portion of text and tap the bold button, it returns an attribute and adds it to attributedText and then to the textView (textView.attributedText = attributedText).
The following is my return function:
func boldText() -> Dictionary<NSAttributedString.Key, Any> {
let attributes: [NSAttributedString.Key: Any] = [
.font: UIFont.systemFont(ofSize: 15, weight: .bold),
]
return attributes
}
Now, here's where things get confusing. If I manually type some text in the UTextView and highlight a portion of that text and tap the Bold button, the app will crash due to the text being out of range, this makes sense as my attributedText variable is only holding the previous dummy text I added in ViewDidLoad and not the new text I have typed.
So my question is, how do I add the new text I typed to the attributedText variable without messing up all the other formatting that was already part of that text?
If I just add the textView text to the variable:
attributedText = NSMutableAttributedString(string: "\(textView.text!)" )
Then obviously it simply overwrites anything that was held in the attributedText variable previously which in turn removes all formatting.
Am I on the right lines in the fact that every time change happens in the textView I need to somehow update my attributedText variable or should I be using a different approach altogether?
I am using NSAttributedString (and NSMutableAttributeString) for writing to a PDF via UIGraphicsBeginPDFContextToFile. When I output an attributed string with underline attributes, the underline is broken when a letters descender goes over underline.
Here is a sample (screen capture) showing the current output:
And here is the code that builds that sample attributed string:
NSAttributedString* ftype =
[[NSMutableAttributedString alloc]
initWithString:#"Dangerous"
attributes:#{
NSParagraphStyleAttributeName:pstyle,
NSFontAttributeName:[UIFont fontWithName:#"TimesNewRomanPS-BoldMT" size:48.0],
NSForegroundColorAttributeName:[UIColor redColor],
NSUnderlineStyleAttributeName:[NSNumber numberWithInt:NSUnderlineStyleSingle]
}
];
My client's and my preference is that the underline be continuous, and ideally shifted below the descender.
Manually drawing the underline is difficult as we would have to computer the text position of the words after layout (sometimes the text is not as simple as the example above).
Does anyone have a fix to put the underline either a) lower or b) make it continuous?
Thanks in advance.
I know this was old and after searching found no answers. The solution I found is in the dictionary you pass in for attributes to NSAttributedString initWithString:attributes: add this
NSExpansionAttributeName : #-0.0001f
This scales the glyph, and it seems even with this small amount the underline doesn't get cut (well it didn't with the 13pt font I was using). Though this doesn't seem like the intended use of NSExpansionAttributeName, but couldn't find another solution.
So I have a UILabel and I've set it's line break mode to clip, but I don't see the ... at the end, instead it just truncates/cut the text at the point where it overflows. Is there any other part of the code that I need to set?
Here's how I am doing it:
[self.newsFeedHeadingTitle_ setLineBreakMode:UILineBreakModeClip];
Set it to "UILineBreakModeTailTruncation" if you want the dots at the end of the string.
You also have to set the number of lines the UILabel is allowed to have. Like this [myLabel setNumberOfLines:x] where x is the number of lines you want to have. If you set x to zero , the label can have as many lines as it needs. By default, that value it's 1 so that is why your label does not break the text into multiple lines.
Hope this helps.
Cheers!
If you're using plain NSString-objects on a UILabel, myLabel.lineBreakMode = NSLineBreakByTruncatingTail; should work.
If you are using NSAttributedString-objects with a NSParagraphStyle, use myParagraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
Setting the linebreak-mode of the paragraph-style worked for me.
I have seen questions posted on here asking how to left and right align two lumps of text, but how can I neatly left and right justify an entire paragraph inside a multiline cell? (Such as what MS Word etc would do if you click on the justification button so that left and right sides of the text are always aligned).
You can also create a custom cell with label which IBOutlet to this class that can be downloaded on github OHAttributedLabel, then try the code below
cell.label.textAlignment = UITextAlignmentJustify;
Is this what you're looking for?
yourTextLabel.textAlignment = UITextAlignmentCenter;
Along with the frame size that you want for your text label.
I display texts in a cell of the UITableView control. Now the texts that I display can be large enough to fit inside one cell width, so it displays the texts on the second line. But it breaks the word in the middle, (like if say "That sounds great, I will pick you up from the abc station", it will display "That sounds great, I will pick you up from the abc sta" on the first line and "tion" on the second line. However, I wanted to be like "That sounds great, I will pick you up from the abc" on the first line and "station" on the second line).
Could you let me know how can I do that?
Thanks.
I think you can achieve this by setting the lineBreakMode on the UILabel.
// setup cell 3.0 SDK code
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.text = #"this is some really long string....";