Center and bold part of a text in Swift - ios

I want to center and bold the title of my paragraph. So far I can make the title bold but I cant center it.
Here's my code:
let title = "Title of Paragraph"
let attrs = [NSFontAttributeName : UIFont.boldSystemFontOfSize(15)]
let boldString = NSMutableAttributedString(string:title, attributes:attrs)
let normalText = "Something here.............."
let attributedString = NSMutableAttributedString(string:normalText)
boldString.appendAttributedString(attributedString)
label.attributedText = boldString
I tried to add another attribue in attrs:
let attrs = [NSFontAttributeName : UIFont.boldSystemFontOfSize(15), NSTextAlignment: NSTextAlignment.Center]
I'm not sure if that's the correct way to center it, but it still gives an error "Type of expression is ambiguous without more context"
I've searched the error but it seems I still can't fix the problem

I think the problem is with the NSTextAlignment key that you are trying to add on the dictionary. It's type is Int which becomes ambiguous with the previous key that is a String so I guess that's why the compiler is complaining. Either way NSTextAlignment is not a valid key to be used on NSMutableAttributedString attributes initialiser.
Maybe you are looking for something like that:
let string = "Any String"
let style = NSMutableParagraphStyle()
style.alignment = .Center
let attributes = [
NSFontAttributeName: UIFont.boldSystemFontOfSize(15),
NSParagraphStyleAttributeName: style
]
let attributedString = NSMutableAttributedString(string: string, attributes: attributes)

Try a category on UILabel:
UILabel+boldText
func boldSubstring(substring: String) {
var range: NSRange = self.text!.rangeOfString(substring)
var attributedText: NSMutableAttributedString = NSMutableAttributedString(attributedString: self.attributedText)
attributedText.setAttributes([NSFontAttributeName: UIFont.boldSystemFontOfSize(self.font.pointSize)], range: range)
self.attributedText = attributedText
}
func centerAlginement(substring:String) {
var paragraphStyle: NSMutableParagraphStyle = NSMutableParagraphStyle.new
paragraphStyle.alignment = .Center
var attributedString: NSAttributedString = NSAttributedString.alloc(string: substring, attributes: [NSParagraphStyleAttributeName: paragraphStyle])
self.attributedText = attributedString
}
now you can use it in your view import category class to your view
myLabel.text = "DemoText"
myLabel.boldSubstring(myLabel.text)
myLabel.centerAlginement(myLabel.text)

Related

How to not have an "expanded" textview?

I am encountering a small problem that is that my label text is expanded as you can see on the picture below:
I would like to know how to get normal text please.
If you know how to do that, please let me know.
Here is the code :
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let appDelegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.updateUserActivityOnApp(isFirstConnection: false)
//print(self.view.frame.size.height - (self.extraPhoto1.frame.size.height + self.extraPhoto1.frame.origin.y))
if self.view.frame.size.height - (self.extraPhoto1.frame.size.height + self.extraPhoto1.frame.origin.y) >= 140 {
descriptionTopSpaceConstraint.constant = 20
self.view.layoutIfNeeded()
}
profilePicture.image = nil
descriptionTxtView.isEditable = false
descriptionTxtView.text! = ""
extraPhoto1.image = nil
extraPhoto2.image = nil
extraPhoto3.image = nil
usernameCountryLbl.text! = "Loading..."
self.navigationItem.setHidesBackButton(true, animated:true)
var attrStr = NSMutableAttributedString(string: "this is some very long test string for justification. this is some very long test string for justification. this is some very long test string for justification. this is some very long test string for justification")
var paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = NSTextAlignment.justified
paragraphStyle.hyphenationFactor = 1
attrStr.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: NSMakeRange(0, attrStr.length))
attrStr.addAttribute(NSAttributedString.Key.kern, value: -0.1, range: NSMakeRange(0, attrStr.length))
self.descriptionTxtView.attributedText = attrStr // label is the UILabel where you want the text to be displayed
self.descriptionTxtView.sizeToFit()
}
Thank you very much
textAligment.natural as #Rob mentioned is one of the options in case you don't care about your text being justified. Otherwise the only way is to use NSMutableAttributedString and hyphenationFactor. In case you need it to be justified you can do something like this:
var attrStr = NSMutableAttributedString(string: "this is some very long test string for justification. this is some very long test string for justification. this is some very long test string for justification. this is some very long test string for justification")
var paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = NSTextAlignment.justified
paragraphStyle.hyphenationFactor = 1
attrStr.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: NSMakeRange(0, attrStr.length))
attrStr.addAttribute(NSAttributedString.Key.kern, value: -0.1, range: NSMakeRange(0, attrStr.length))
label.attributedText = attrStr // label is the UILabel where you want the text to be displayed
label.sizeToFit()
label.numberOfLines = 0
Set your text view's textAlignment to .natural instead of .justified.

Attributed String only to change Font Size, not the font name

I am trying to set attributed text to a label. However, I want to change the size but not want to set the font name, how do I do that?
let myAttributedString = NSMutableAttributedString(string: "Some Text",
attributes: [NSFontAttributeName:UIFont(name: "Georgia",size: 18.0)!])
You can use this,
Your_Label.font.withSize(20)
Your end string will be,
let myAttributedString = NSMutableAttributedString(string: "Some Text", attributes: [NSFontAttributeName:Your_Label.font.withSize(15)])
Get the font name from label.
let myAttributedString = NSMutableAttributedString(string: "Some Text", attributes: [NSFontAttributeName:UIFont(name: label.fontName, size: 18.0)!])
Swift 5 Extension for changing just the font size in an NSMutableAttributedString
extension NSMutableAttributedString {
/// Changes just the font size of an attributed string, while keeping all other attributes.
func changFontSizeTo(fontSize: Int) {
let attributes = self.attributes(at: 0, effectiveRange: nil)
guard let font = attributes[NSAttributedString.Key.font] as? UIFont else { return }
let newFont = font.withSize(CGFloat(fontSize))
self.addAttributes([NSAttributedString.Key.font: newFont], range: NSRange(location: 0, length: self.string.count))
}
}
UIFont has an instance method withSize(_:), returns a new font that is identical to the current font except the specified size.
Try it to update size of your font.
let label = UILabel()
label.font = UIFont.systemFont(ofSize: 10)
let labelFont = label.font.withSize(20)
Swift 3:
let myAttributedString = NSMutableAttributedString(string: "Some Text", attributes: [NSFontAttributeName:labelFont])
Swift 4:
let myAttributedString = NSMutableAttributedString(string: "Some Text", attributes: [NSAttributedStringKey.font:labelFont])

Change color of a certain word in UILabel

I just want to change color of some word in my Label's string.
What I want to do is:
How Im trying to do:
But it doesnt work and it says: 'NSMutableRLEArray replaceObjectsInRange:withObject:length:: Out of bounds'
So I need an advice to do this easy thing, and I need a trick to do this. You guys can send your way.
Try this, your attributed string has no value when you try get the range of the string. You got to give the attributed string a string value before that. You
let descriptionLabel: UILabel = {
let label = UILabel()
// String variable containing the text.
let fullString = "mehmet alper tabak"
// Choose wwhat you want to be colored.
let coloredString = "alper"
// Get the range of the colored string.
let rangeOfColoredString = (fullString as NSString).range(of: coloredString)
// Create the attributedString.
let attributedString = NSMutableAttributedString(string:fullString)
attributedString.setAttributes([NSForegroundColorAttributeName: UIColor.red],
range: rangeOfColoredString)
label.attributedText = attributedString
return label
}()
descriptionLabel.frame = CGRect(x: 50, y: 50, width: 140, height: 100)
self.view.addSubview(descriptionLabel)
Or you can do an extension of UILabel.
extension UILabel {
func colorString(text: String?, coloredText: String?, color: UIColor? = .red) {
let attributedString = NSMutableAttributedString(string: text!)
let range = (text! as NSString).range(of: coloredText!)
attributedString.setAttributes([NSForegroundColorAttributeName: color!],
range: range)
self.attributedText = attributedString
}
To use it.
self.testLabel.colorString(text: "mehmet alper tabak",
coloredText: "alper")
It is out of bounds because myMutableString is empty at the time that you are trying to set attributes. Try instead:
let myMutableString = NSMutableAttributedString.init(string: label.text)
Create attributedString1 with first color
let attributedString1 = NSAttributedString(string: "robert plant ", attributes: [NSForegroundColorAttributeName: color1])
Create attributedString2 with second color
let attributedString1 = NSAttributedString(string: "with queen", attributes: [NSForegroundColorAttributeName: color2])
Add them to myMutableString
myMutableString.append(attributedString1)
myMutableString.append(attributedString1)

Color up text in textview within a specific range [duplicate]

The issue I am having is that I want to be able to change the textColor of certain text in a TextView. I am using a concatenated string, and just want the strings I am appending into the TextView's text. It appears that what I want to use is NSMutableAttributedString, but I am not finding any resources of how to use this in Swift. What I have so far is something like this:
let string = "A \(stringOne) with \(stringTwo)"
var attributedString = NSMutableAttributedString(string: string)
textView.attributedText = attributedString
From here I know I need to find the range of words that need to have their textColor changed and then add them to the attributed string. What I need to know is how to find the correct strings from the attributedString, and then change their textColor.
Since I have too low of a rating I can't answer my own question, but here is the answer I found
I found my own answer by translating from translating some code from
Change attributes of substrings in a NSAttributedString
Here is the example of implementation in Swift:
let string = "A \(stringOne) and \(stringTwo)"
var attributedString = NSMutableAttributedString(string:string)
let stringOneRegex = NSRegularExpression(pattern: nameString, options: nil, error: nil)
let stringOneMatches = stringOneRegex.matchesInString(longString, options: nil, range: NSMakeRange(0, attributedString.length))
for stringOneMatch in stringOneMatches {
let wordRange = stringOneMatch.rangeAtIndex(0)
attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.nameColor(), range: wordRange)
}
textView.attributedText = attributedString
Since I am wanting to change the textColor of multiple Strings I will make a helper function to handle this, but this works for changing the textColor.
let mainString = "Hello World"
let stringToColor = "World"
SWIFT 5
let range = (mainString as NSString).range(of: stringToColor)
let mutableAttributedString = NSMutableAttributedString.init(string: mainString)
mutableAttributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.red, range: range)
textField = UITextField.init(frame: CGRect(x:10, y:20, width:100, height: 100))
textField.attributedText = mutableAttributedString
SWIFT 4.2
let range = (mainString as NSString).range(of: stringToColor)
let mutableAttributedString = NSMutableAttributedString.init(string: mainString)
mutableAttributedString.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.red, range: range)
textField = UITextField.init(frame: CGRect(x:10, y:20, width:100, height: 100))
textField.attributedText = mutableAttributedString
I see you have answered the question somewhat, but to provide a slightly more concise way without using regex to answer to the title question:
To change the colour of a length of text you need to know the start and end index of the coloured-to-be characters in the string e.g.
var main_string = "Hello World"
var string_to_color = "World"
var range = (main_string as NSString).rangeOfString(string_to_color)
Then you convert to attributed string and use 'add attribute' with NSForegroundColorAttributeName:
var attributedString = NSMutableAttributedString(string:main_string)
attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor() , range: range)
A list of further standard attributes you can set can be found in Apple's documentation
Swift 2.1 Update:
let text = "We tried to make this app as most intuitive as possible for you. If you have any questions don't hesitate to ask us. For a detailed manual just click here."
let linkTextWithColor = "click here"
let range = (text as NSString).rangeOfString(linkTextWithColor)
let attributedString = NSMutableAttributedString(string:text)
attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor() , range: range)
self.helpText.attributedText = attributedString
self.helpText is a UILabel outlet.
Swift 4.2 and Swift 5 colorise parts of the string.
A very easy way to use NSMutableAttributedString while extending the String. This also can be used to colourize more than one word in the whole string.
import UIKit
extension String {
func attributedStringWithColor(_ strings: [String], color: UIColor, characterSpacing: UInt? = nil) -> NSAttributedString {
let attributedString = NSMutableAttributedString(string: self)
for string in strings {
let range = (self as NSString).range(of: string)
attributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: color, range: range)
}
guard let characterSpacing = characterSpacing else {return attributedString}
attributedString.addAttribute(NSAttributedString.Key.kern, value: characterSpacing, range: NSRange(location: 0, length: attributedString.length))
return attributedString
}
}
Now you can use globally at any viewcontroller you want:
let attributedWithTextColor: NSAttributedString = "Doc, welcome back :)".attributedStringWithColor(["Doc", "back"], color: UIColor.black)
myLabel.attributedText = attributedWithTextColor
Answer is already given in previous posts but i have a different way of doing this
Swift 3x :
var myMutableString = NSMutableAttributedString()
myMutableString = NSMutableAttributedString(string: "Your full label textString")
myMutableString.setAttributes([NSFontAttributeName : UIFont(name: "HelveticaNeue-Light", size: CGFloat(17.0))!
, NSForegroundColorAttributeName : UIColor(red: 232 / 255.0, green: 117 / 255.0, blue: 40 / 255.0, alpha: 1.0)], range: NSRange(location:12,length:8)) // What ever range you want to give
yourLabel.attributedText = myMutableString
Hope this helps anybody!
Chris' answer was a great help to me, so I used his approach and turned into a func that I can reuse. This let's me assign a color to a substring while giving the rest of the string another color.
static func createAttributedString(fullString: String, fullStringColor: UIColor, subString: String, subStringColor: UIColor) -> NSMutableAttributedString
{
let range = (fullString as NSString).rangeOfString(subString)
let attributedString = NSMutableAttributedString(string:fullString)
attributedString.addAttribute(NSForegroundColorAttributeName, value: fullStringColor, range: NSRange(location: 0, length: fullString.characters.count))
attributedString.addAttribute(NSForegroundColorAttributeName, value: subStringColor, range: range)
return attributedString
}
Swift 4.1
NSAttributedStringKey.foregroundColor
for example if you want to change font in NavBar:
self.navigationController?.navigationBar.titleTextAttributes = [ NSAttributedStringKey.font: UIFont.systemFont(ofSize: 22), NSAttributedStringKey.foregroundColor: UIColor.white]
You can use this extension
I test it over
swift 4.2
import Foundation
import UIKit
extension NSMutableAttributedString {
convenience init (fullString: String, fullStringColor: UIColor, subString: String, subStringColor: UIColor) {
let rangeOfSubString = (fullString as NSString).range(of: subString)
let rangeOfFullString = NSRange(location: 0, length: fullString.count)//fullString.range(of: fullString)
let attributedString = NSMutableAttributedString(string:fullString)
attributedString.addAttribute(NSAttributedStringKey.foregroundColor, value: fullStringColor, range: rangeOfFullString)
attributedString.addAttribute(NSAttributedStringKey.foregroundColor, value: subStringColor, range: rangeOfSubString)
self.init(attributedString: attributedString)
}
}
Swift 2.2
var myMutableString = NSMutableAttributedString()
myMutableString = NSMutableAttributedString(string: "1234567890", attributes: [NSFontAttributeName:UIFont(name: kDefaultFontName, size: 14.0)!])
myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor(red: 0.0/255.0, green: 125.0/255.0, blue: 179.0/255.0, alpha: 1.0), range: NSRange(location:0,length:5))
self.lblPhone.attributedText = myMutableString
Easiest way to do label with different style such as color, font etc. is use property "Attributed" in Attributes Inspector. Just choose part of text and change it like you want
Based on the answers before I created a string extension
extension String {
func highlightWordsIn(highlightedWords: String, attributes: [[NSAttributedStringKey: Any]]) -> NSMutableAttributedString {
let range = (self as NSString).range(of: highlightedWords)
let result = NSMutableAttributedString(string: self)
for attribute in attributes {
result.addAttributes(attribute, range: range)
}
return result
}
}
You can pass the attributes for the text to the method
Call like this
let attributes = [[NSAttributedStringKey.foregroundColor:UIColor.red], [NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: 17)]]
myLabel.attributedText = "This is a text".highlightWordsIn(highlightedWords: "is a text", attributes: attributes)
Swift 4.1
I have changed from this
In Swift 3
let str = "Welcome "
let welcomeAttribute = [ NSForegroundColorAttributeName: UIColor.blue()]
let welcomeAttrString = NSMutableAttributedString(string: str, attributes: welcomeAttribute)
And this in Swift 4.0
let str = "Welcome "
let welcomeAttribute = [ NSAttributedStringKey.foregroundColor: UIColor.blue()]
let welcomeAttrString = NSMutableAttributedString(string: str, attributes: welcomeAttribute)
to Swift 4.1
let str = "Welcome "
let welcomeAttribute = [ NSAttributedStringKey(rawValue: NSForegroundColorAttributeName): UIColor.blue()]
let welcomeAttrString = NSMutableAttributedString(string: str, attributes: welcomeAttribute)
Works fine
swift 4.2
let textString = "Hello world"
let range = (textString as NSString).range(of: "world")
let attributedString = NSMutableAttributedString(string: textString)
attributedString.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.red, range: range)
self.textUIlable.attributedText = attributedString
This might be work for you
let main_string = " User not found,Want to review ? Click here"
let string_to_color = "Click here"
let range = (main_string as NSString).range(of: string_to_color)
let attribute = NSMutableAttributedString.init(string: main_string)
attribute.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.blue , range: range)
lblClickHere.attributedText = attribute
With this simple function you can assign the text and highlight the chosen word.
You can also change the UITextView to UILabel, etc.
func highlightBoldWordAtLabel(textViewTotransform: UITextView, completeText: String, wordToBold: String){
textViewToTransform.text = completeText
let range = (completeText as NSString).range(of: wordToBold)
let attribute = NSMutableAttributedString.init(string: completeText)
attribute.addAttribute(NSAttributedString.Key.font, value: UIFont.boldSystemFont(ofSize: 16), range: range)
attribute.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.black , range: range)
textViewToTransform.attributedText = attribute
}
For everyone who are looking for "Applying specific color to multiple words in text", we can do it using NSRegularExpression
func highlight(matchingText: String, in text: String) {
let attributedString = NSMutableAttributedString(string: text)
if let regularExpression = try? NSRegularExpression(pattern: "\(matchingText)", options: .caseInsensitive) {
let matchedResults = regularExpression.matches(in: text, options: [], range: NSRange(location: 0, length: attributedString.length))
for matched in matchedResults {
attributedString.addAttributes([NSAttributedStringKey.backgroundColor : UIColor.yellow], range: matched.range)
}
yourLabel.attributedText = attributedString
}
}
Reference link : https://gist.github.com/aquajach/4d9398b95a748fd37e88
You can use as simple extension
extension String{
func attributedString(subStr: String) -> NSMutableAttributedString{
let range = (self as NSString).range(of: subStr)
let attributedString = NSMutableAttributedString(string:self)
attributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.red , range: range)
return attributedString
}
}
myLable.attributedText = fullStr.attributedString(subStr: strToChange)
This extension works well when configuring the text of a label with an already set default color.
public extension String {
func setColor(_ color: UIColor, ofSubstring substring: String) -> NSMutableAttributedString {
let range = (self as NSString).range(of: substring)
let attributedString = NSMutableAttributedString(string: self)
attributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: color, range: range)
return attributedString
}
}
For example
let text = "Hello World!"
let attributedText = text.setColor(.blue, ofSubstring: "World")
let myLabel = UILabel()
myLabel.textColor = .white
myLabel.attributedText = attributedText
Super easy way to do this.
let text = "This is a colorful attributed string"
let attributedText =
NSMutableAttributedString.getAttributedString(fromString: text)
attributedText.apply(color: .red, subString: "This")
//Apply yellow color on range
attributedText.apply(color: .yellow, onRange: NSMakeRange(5, 4))
For more detail click here:
https://github.com/iOSTechHub/AttributedString
To change color of the font colour, first select attributed instead of plain like in the image below
You then need to select the text in the attributed field and then select the color button on the right-hand side of the alignments. This will change the color.
You can use this method. I implemented this method in my common utility class to access globally.
func attributedString(with highlightString: String, normalString: String, highlightColor: UIColor) -> NSMutableAttributedString {
let attributes = [NSAttributedString.Key.foregroundColor: highlightColor]
let attributedString = NSMutableAttributedString(string: highlightString, attributes: attributes)
attributedString.append(NSAttributedString(string: normalString))
return attributedString
}
If you are using Swift 3x and UITextView, maybe the NSForegroundColorAttributeName won't work (it didn't work for me no matter what approach I tried).
So, after some digging around I found a solution.
//Get the textView somehow
let textView = UITextView()
//Set the attributed string with links to it
textView.attributedString = attributedString
//Set the tint color. It will apply to the link only
textView.tintColor = UIColor.red
You need to change textview parameters, not parameters of attributed string
textView.linkTextAttributes = [
NSAttributedString.Key.foregroundColor: UIColor.red,
NSAttributedString.Key.underlineColor: UIColor.red,
NSAttributedString.Key.underlineStyle: NSUnderlineStyle.single.rawValue
]
Please check cocoapod Prestyler:
Prestyler.defineRule("$", UIColor.orange)
label.attributedText = "This $text$ is orange".prestyled()
extension String{
// to make text field mandatory * looks
mutating func markAsMandatoryField()-> NSAttributedString{
let main_string = self
let string_to_color = "*"
let range = (main_string as NSString).range(of: string_to_color)
print("The rang = \(range)")
let attribute = NSMutableAttributedString.init(string: main_string)
attribute.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.rgbColor(red: 255.0, green: 0.0, blue: 23.0) , range: range)
return attribute
}
}
use
EmailLbl.attributedText = EmailLbl.text!.markAsMandatoryField()

Swift - NSMutableAttributedString changes all text in UITextView

Download an example I have created
Example Link
I am adding a link to my UITextView like this:
let systemFont = self.text.font!
let linkAttributes = [
NSFontAttributeName : systemFont,
NSLinkAttributeName: NSURL(string: alertController.textFields![0].text!)!] as [String : Any]
let myAttributes2 = [ NSForegroundColorAttributeName: customGreen]
let attributedString = NSMutableAttributedString(string: "\(urlName)")
// Set the 'click here' substring to be the link
attributedString.setAttributes(linkAttributes, range: NSMakeRange(0, urlName.characters.count))//(0, urlName.characters.count))
self.text.linkTextAttributes = myAttributes2
self.text.textStorage.insert(attributedString, at: self.text.selectedRange.location)
let cursor = NSRange(location: self.text.selectedRange.location + "\(urlName)".characters.count, length: 0)
self.text.selectedRange = cursor
// self.text.font = systemFont
But after inserting it, it changes all the current text in the UITextView to the same font.
So for example if I have some text that is Bold and some more text that is Italic, after I add the url(which is system font) it resets all the bold/italic text to the system font....
If anyone knows how to keep the current font of the previous text I'd really appreciate the help.
For any further explanation just drop a comment below!
Many thanks in advance to anyone that can help!
Update
I am changing the text here in textDidChange
if text == " "
{
// let systemFont = self.text.font!
// let linkAttributes = [NSFontAttributeName : systemFont]
let attributes = [NSForegroundColorAttributeName: UIColor.black, NSFontAttributeName: self.text.font!] as [String : Any]
let attributedString = NSMutableAttributedString(string: text, attributes: attributes)
self.text.textStorage.insert(attributedString, at: range.location)
let cursor = NSRange(location: self.text.selectedRange.location+1, length: 0)
textView.selectedRange = cursor
return false
}
I have that so after adding the URL I make a space and reset the font so I don't continue typing as a URL link...Probably the problem but when I type normal and don't set a url link the text doesn't changing while make a space...
let urlName = "google"
#IBAction func btnPressed(_ sender: Any) {
let systemFont = UIFont.systemFont(ofSize: 11)
let linkAttributes = [
NSFontAttributeName : systemFont,
NSLinkAttributeName: NSURL(string: "https://www.google.com")!] as [String : Any]
let myAttributes2 = [NSForegroundColorAttributeName: UIColor.green]
let attributedString = NSMutableAttributedString(string: "\(urlName)")
attributedString.setAttributes(linkAttributes, range: NSMakeRange(0, urlName.characters.count))//(0, urlName.characters.count))
self.text.linkTextAttributes = myAttributes2
self.text.textStorage.insert(attributedString, at: self.text.selectedRange.location)
let cursor = NSRange(location: self.text.selectedRange.location + "\(urlName)".characters.count, length: 0)
self.text.selectedRange = cursor
}
This is how you should update the textView if you want to add text programmatically.
Do not use the textViewDidChange(_:) delegate method in this situation.
UPDATE
I've downloaded your code from dropbox and made some changes to it.
so here I'm changing the textView text in viewDidLoad() for example.
I've created two constants in order to use them again in the code, fontAttr is the original font with the name and size and fontColorAttrBlue is the original font color.
let fontAttr = UIFont(name: "Helvetica-Bold", size: 20)
let fontColorAttrBlue = UIColor.blue
override func viewDidLoad() {
super.viewDidLoad()
let attrString = NSMutableAttributedString(string: "Hello World, How are you?", attributes: [NSFontAttributeName : fontAttr!, NSForegroundColorAttributeName: fontColorAttrBlue ])
self.textView.delegate = self // you can set the delegate from the storyboard.
self.textView.attributedText = attrString
}
And I've deleted this line of code self.textView.font = systemFont from webLink(_ sender: AnyObject) action method, so that it wouldn't change the font of the textView.
And lastly in textView(_:shouldChangeTextIn:replacementText:) method instead of checking if the user has entered " " String I'm checking if the user has entered any String and reusing the font attributes that I created in the beginning, so that if the user enters any kind of text after the link it would be the original text and not the one assigned for the like text.
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if text.characters.count > 0 {
let attributedString = NSMutableAttributedString(string: text, attributes: [NSFontAttributeName : self.fontAttr!, NSForegroundColorAttributeName: self.fontColorAttrBlue])
self.textView.textStorage.insert(attributedString, at: range.location)
let cursor = NSRange(location: self.textView.selectedRange.location+1, length: 0)
textView.selectedRange = cursor
return false
}
return true
}

Resources