nsmutableattributedstring

Highlight just the text in a UILabel

本小妞迷上赌 提交于 2019-11-26 16:17:40
问题 I'm attempting to set the background color/highlight just the text within a UILabel . The issue is that the line breaks and spaces added to the UILabel to keep the text centered are also being highlighted. Notice the spacing before the last line in the UILabel is highlighted. Also, the beginning and end of any new lines are also highlighted. I'm creating the example above with the following code: -(void)createSomeLabel { // Create and position my label UILabel *someLabel = [[UILabel alloc]

NSMutableAttributedString add different alignments

心已入冬 提交于 2019-11-26 16:05:14
问题 Is it possible to add left and right alignments to different parts of the string? I tried to add alignment attribute to the right part: NSMutableParagraphStyle *paragrahStyle = [[NSMutableParagraphStyle alloc] init]; [paragrahStyle setAlignment:NSTextAlignmentRight]; [mutableAttributedString addAttribute:NSParagraphStyleAttributeName value:paragrahStyle range:rangeOfDate]; But the whole string is aligned to the left. 回答1: What you do in your code is to set the paragraph style (the text from

Changing specific text's color using NSMutableAttributedString in Swift

让人想犯罪 __ 提交于 2019-11-26 14:21:15
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

Making text bold using attributed string in swift

淺唱寂寞╮ 提交于 2019-11-26 14:16:54
I have a string like this var str = "@text1 this is good @text1" Now replace text1 with another string, say t 1 . I am able to replace the text, but i am not able to bold it. I want to bold the new string t 1 , so that the final output will be: @t 1 this is good @t 1 How can I do it? All the examples I am seeing are in Objective-C, but I want to do it in Swift. Thanks in advance. Here is a neat way to make a combination of bold and normal texts in a single label. Extension: Swift 3.0 extension NSMutableAttributedString { @discardableResult func bold(_ text:String) -> NSMutableAttributedString

Color all occurrences of string in swift

不羁岁月 提交于 2019-11-26 12:31:29
问题 This code var textSearch=\"hi\" var textToShow=\"hi hihi hi\" var rangeToColor = (textToShow as NSString).rangeOfString(textSearch) var attributedString = NSMutableAttributedString(string:textToShow) attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.yellowColor() , range: rangeToColor) TextView.attributedText=attributedString gives me NSRange to color a string inside the TextView. The problem is that I only returns the first occurrence. If the word contains \"hi

Changing specific text's color using NSMutableAttributedString in Swift

ε祈祈猫儿з 提交于 2019-11-26 03:51:46
问题 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)

Making text bold using attributed string in swift

不问归期 提交于 2019-11-26 03:08:58
问题 I have a string like this var str = \"@text1 this is good @text1\" Now replace text1 with another string, say t 1 . I am able to replace the text, but i am not able to bold it. I want to bold the new string t 1 , so that the final output will be: @t 1 this is good @t 1 How can I do it? All the examples I am seeing are in Objective-C, but I want to do it in Swift. Thanks in advance. 回答1: Here is a neat way to make a combination of bold and normal texts in a single label. Extension: Swift 3.0