hyperlink not highlighted in UILabel with NSAttributedText in iOS - ios

I am displaying a label with an attributed text. The label contains some hyper links.
I understand that these are only clickable in a textView however I thought that they would appear blue and underlined in a UILabel which has NSAttributedText.
In my case the link is not any different than the other text (not blue or underlined). Is there any property of the UILabel I need to change to make the link appear blue inside a UILabel?
let style = NSMutableParagraphStyle()
style.lineBreakMode = NSLineBreakMode.ByWordWrapping
let attributes = [NSFontAttributeName: self.defaultFont(), NSParagraphStyleAttributeName: style]
let attributedString = NSAttributedString(string: "www.somelink.com", attributes: attributes)
label.attributedText = attributedString

You need to add NSLinkAttributeName attribute to your NSAttributedString like this:
attributedString.addAttribute(NSLinkAttributeName, value: "www.somelink.com", range: attributedString.string.rangeOfString("www.somelink.com"))
Please check the range for your needs.

You need NSLinkAttributeName. Above answer should work.
let style = NSMutableParagraphStyle()
style.lineBreakMode = NSLineBreakMode.ByWordWrapping
let attributes = [NSFontAttributeName: self.defaultFont(), NSParagraphStyleAttributeName: style]
let attributedString.addAttribute(NSLinkAttributeName, value: "www.somelink.com", range: attributedString.string.rangeOfString("www.somelink.com"))
label.attributedText = attributedString

Related

Adding font size and custom font name to attributed string in UITextView

I have the following code to add the attributed string to the UITextView text. I need to add the custom font and font size to it in swift. How to do it?
func setAttributedString(string: String) -> NSAttributedString {
let attrString = NSMutableAttributedString(string: string)
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 7
paragraphStyle.minimumLineHeight = 7
attrString.addAttribute(NSParagraphStyleAttributeName, value:paragraphStyle, range:NSRange(location: 0, length:attrString.length))
attrString.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 30), range: NSRange(location: 0, length:attrString.length))
return attrString
}
I need to add the custom font and font size to the following function. How to achieve this?
In Swift 3
let attributes = [NSAttributedStringKey.font: UIFont(name: "HelveticaNeue-Bold", size: 17)!,
NSAttributedStringKey.foregroundColor: UIColor.white]
let attributedString = NSMutableAttributedString(string: "Your string" , attributes: attributes)
Use the initializer init(string:attributes:)
https://developer.apple.com/documentation/foundation/nsattributedstring/1408136-init
let attrString = NSMutableAttributedString(string: string, attributes:
[NSFontAttributeName:UIFont(
name: "Georgia",
size: 18.0)!])
//Add more attributes here

How to center vertically attributed text (custom placeholder font) in UITextField?

I am using attributed text property to set custom placeholder font in UITExtfield. But my placeholder text is not vertically centered.Here is preview
let font = UIFont(name: Font.myFont.name, size: 15)!
let attributes = [
NSForegroundColorAttributeName: UIColor.cadetGrey,
NSFontAttributeName: font
]
exampleTextField.attributedPlaceholder = NSAttributedString(string: "Example Placeholder",
attributes: attributes)
I tried to add
let centeredParagraphStyle = NSMutableParagraphStyle()
centeredParagraphStyle.alignment = .center
to attribute still no luck. I also tried to use SizeToFit() on my UITextBox but no luck at all
This worked for me in Swift 5:
let unitText = NSAttributedString(string: "\(unit)", attributes: [NSAttributedString.Key.foregroundColor: MyColors.doveGray, NSAttributedString.Key.baselineOffset:2])
Try like this :
let centeredParagraphStyle = NSMutableParagraphStyle()
centeredParagraphStyle.alignment = .center
let attributes: [String : Any] = [NSParagraphStyleAttributeName: centeredParagraphStyle]
This is will set both placeholder and text input by user in center.
exampleTextField.textAlignment = .center
To center both vertically and horizontally.
exampleTextField.contentVerticalAlignment = UIControlContentVerticalAlignment.center
exampleTextField.textAlignment = .center
OR
This is will only set placeholder in center whereas the text input by user will remain at it's default position i.e left align
let font = UIFont(name: Font.myFont.name, size: 15)!
let centeredParagraphStyle = NSMutableParagraphStyle()
centeredParagraphStyle.alignment = .center
// add attribute of NSMutableParagraphStyle
let attributes = [
NSForegroundColorAttributeName: UIColor.gray,
NSFontAttributeName: font,
NSParagraphStyleAttributeName: centeredParagraphStyle
]
exampleTextField.attributedPlaceholder = NSAttributedString(string: "Example Placeholder",
attributes: attributes)

How can i change Navigation Title color, Character spacing & Font style from Storyboard for iOS?

I am able to change character spacing, title color & font style programmatically. Here is the sample code
let titleLabel = UILabel()
let colour = UIColor.redColor()
let attributes: [NSString : AnyObject] = [NSFontAttributeName: UIFont.systemFontOfSize(12), NSForegroundColorAttributeName: colour, NSKernAttributeName : 5.0]
titleLabel.attributedText = NSAttributedString(string: "My String", attributes: attributes)
titleLabel.sizeToFit()
self.navigationItem.titleView = titleLabel
but i want to change these three from storyboard. Is there any possible way?
No, it's not possible using Storyboard

Convert UItextview to NSAttributedString

I've a UITextView with different properties like different color in the same text, font and size. I need to pass that text into a NSAttributedString variable.
With that code I'm not able to keep the properties of the text.
How can I do?
let attr = NSAttributedString(string: textView.text)
Use the attributedString initializer of NSAttributedString and pass it your textview attributed text:
let attr = NSAttributedString(attributedString: textView.attributedText)
var myString:NSString = textView.text
var myMutableString = NSMutableAttributedString()
myMutableString = NSMutableAttributedString(string: myString, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 18.0)!])
myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSRange(location:0,length:myString.length))
textView.attributedText = myMutableString
TextView With RedFont Color :

How can I set the attributed text while having no placeholder text?

Code that worked in Xcode 6.2 no longer works and I'm having trouble figuring out how to fix it. String attributes are now ignored unless I set default text. Here's what I've got right now:
let font = UIFont(name: "Arial-BoldMT", size: 42.0)!
let textStyle = NSMutableParagraphStyle.defaultParagraphStyle().mutableCopy() as! NSMutableParagraphStyle
textStyle.alignment = NSTextAlignment.Center
let textColor = UIColor.whiteColor()
var shadow = NSShadow()
shadow.shadowColor = UIColor.blackColor()
shadow.shadowOffset = CGSizeMake(2.0,2.0)
var attr = [String:NSObject]()
attr = [
NSFontAttributeName: font,
NSForegroundColorAttributeName: textColor,
NSParagraphStyleAttributeName: textStyle,
NSShadowAttributeName: shadow
]
let placeholderText = NSAttributedString(string: "", attributes: attr)
textView.attributedText = placeholderText
If you change the placeHolderText to NSAttributedString(string: " ", attributes: attr) you can then change the UITextView's text by changing its text property and the new text will have all the same attributes.
I hope that answers your question.

Resources