Preserve text style when changing Text view text - ios

I have a Text View with attributed text, with some style, set in Xcode utilities pan. Basically, a font, a size, and an alignement.
Then programmatically I change text of the view:
self.myView.text = "New text"
It does change the text, but discard all style of the text and use a default style.
I tried self.myView.attributedText = "New text" but got a protocol conformance issue.
What's the correct strategy to deal with this ?
Is there a way to inject the new text while preserving style ?
Or should I reset style manually each time I change text ?
(please answer in swift if possible)

Basically you need to create an NSAttributedString with the desired text and formatting (font and text alignment).
Then assign the attribute string to the text field's attributedText property.

Related

How to set a "bold" dynamic font type for UILabel?

The Apple documentation says that for iOS an app can use text styles and dynamic font type so text size automatically adjusts to the users reading preferences.
I have a text style Body and set Dynamic Type to true so that it automatically adjust the text size. This works good.
This text I would also like to make appear bold. I haven't found a way yet how to achieve this either by code or in the Interface Builder.
Is there a way to achieve a "bold dynamic font type"?
Ok, it seems the documentation provides a hint. For using custom fonts, I consider a bold font as a custom font now, one can use the UIFontMetrics class.
If I pass a "bold custom font" to the scaledFontForFont method of UIFontMetrics class I get the desired result with the dynamic font type UIFontTextStyleBody.
UIFont* boldSystemFont = [UIFont boldSystemFontOfSize:17];
myLabel.font = [[UIFontMetrics metricsForTextStyle:UIFontTextStyleBody] scaledFontForFont:boldSystemFont];
So far I only see this way using code. No idea if this could somehow be setup in the Interface Builder as well.

UIButton word wrap doesn't work when entering text?

I've got a UIButton, it's a simple segue to another page.
I've set Title to attributed and then selected word wrap. This works fine, the second word wraps down to the next line.
However, it is all left justified. When I select "Align Centre" (using the buttons just under the "Title", the word wrap no longer works and simply runs .... so you can't see it all. (e.g. "next pa" instead of "next page")
Am I missing something here? It seems like such a trivial thing to do! There's an old answer here can't get word wrap to work on UIButton but it's both old and uses code - surely you don't need code to centre the button text if you want to word wrap it to 2 lines!?
I've set Title to attributed and then selected word wrap. This works fine, the second word wraps down to the next line. However, it is all left justified.
Once you've decided to use an attributed string, you must do everything with the attributed string. So give your attributed string a paragraph style that centers the text.
let para = NSMutableParagraphStyle()
para.alignment = .center
para.lineBreakMode = .byWordWrapping
let s = NSAttributedString(
string: "Hello World", attributes: [.paragraphStyle : para])
self.button.setAttributedTitle(s, for: .normal)
You will also need to set the button's title label to allow multiple lines.
self.button.titleLabel?.numberOfLines = 0
Result:
surely you don't need code to centre the button text if you want to word wrap it to 2 lines!?
Not to center it, no; you can set the centering in the storyboard. So you could eliminate the first batch of code and configure it in the storyboard. But you must use code to turn the title label into a multiline label:
self.button.titleLabel?.numberOfLines = 0
There is no way to do that in the storyboard, because you have no access to the title label there.
I've just been playing round with this and I've found it works if you set it to 'character wrap' rather than 'word wrap' once you've selected centre alignment.
If anyone has a better solution please add it, as I guess this might have issues if you slightly change the width etc when using auto layout for different screen sizes etc if you want it to adapt its width so this might not be the best solution but it does work

Custom bulleted text in UITextView

I need square bulleted(colored) text as follows
◼︎ Some large amount of text that
make it span to the next line.
to be shown in UITextView. But the problem is it doesn't git the padding on the next line.
◼︎ Some large amount of text that
make it span to the next line.
These bulleted text is of static content and hence no need to set it through code.
Also custom font is not working when I set attributed type. Custom font used is Raleway.
You need to set up a ruler with a "hanging indent." I have no idea how to do this manually with text attributes.
I would suggest instead setting up what you want in a .rtf file and then loading an attributed string from the RTF using either initWithURL:options:documentAttributes:error: (which is only available in iOS 9 or later) or initWithData:options:documentAttributes:error: (which is available in iOS >= 7.0.)
You specify an options dictionary of
Objective-C:
#{NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType}
Swift:
[NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType]

How to fix UILabel padding issue?

I have a UILabel and whenever I set its outlet the padding I created in Storyboards for it under attributed text disappears like it was never there. The text the stays stuck on the left side. How can I fix this issue?
#IBOutlet weak var mycoollabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
mycoollabel.text = "Wow"
}
Note: OP clarified in a comment that they are setting their padding using attributed text in Interface Builder.
The problem here is a confusion between text and attributed text, or, because these are code, it's text and attributedText.
On iOS, lots of things have a text property, and it's just a simple string of text that gets displayed somewhere without any sort of formatting. Usually the formatting is attached to the object that is showing the text, e.g. a label has a text colour and alignment properties that make its text look however you want.
On iOS, most (all?) of those things also have an attributedText property, which is a very different beast. Attributed text contains a string but also specific formatting instructions required to display that string – text attributes, hence the name.
So, OP is creating an attributed text configuration in their storyboard, then, in code, modifying the text property. This will overwrite their formatted string with a plain string, losing its layout configuration.
If you're looking to create an attributed string with a first line indent, you should try this:
let ps = NSMutableParagraphStyle();
ps.firstLineHeadIndent = 50
let attrs = [NSParagraphStyleAttributeName: ps]
let attributedString = NSAttributedString(string: "Hello, world!", attributes: attrs)
label.attributedText = attributedString
Note that there are a few different options available to you here, so you might need to find the right type of indent for your precise needs. If you right-click on NSMutableParagraphStyle and choose Jump To Definition you'll see all the options.
That's because a UILabel can have either plain text or attributed text. You set attributed text in the storyboard, and add some formatting (padding, etc.). Then, in code, you override that text (and all its formatting) with plain text.
If you want to keep the formatting, you need to set the label's attributedText property. And add all the necessary formatting to the NSAttributedString object you create for that.

iOS: URL link in UITextField with different text

I want to insert a URL hyperlink into a UITextField that has different display text.
This is super easy in html:
Go To Google
How can I do this in iOS?
You can change the text style inside your UITextField via an attributed string sent to the text field's "attributedText" property.
However, what I suspect you really want to do is have a link that's clickable. I would recommend using a UIButton with a custom type (i.e. no border) and you can set the color and the underline style.
But like Evan said, if you have multiple lines of text, it may be smarter to use a UITextField where you set "editable" to NO and turn on the LINK traits (both of these you can do from the object inspector in Xcode).
Alright, so here's what I did to get what I wanted. I used UIWebView, and simply linked it to an html page in the project that has the text, and hyperlink at the bottom with different text displayed.
Answer is here :
If you want it as clickable Hyperlink,
NSString *string = #"Go To Google";
You need to add "BACKWARD SLASH" before ". That's it.

Resources