I am trying to create a strikethrough effect on part of a UILabel text, so fat without success. I have noticed that I can create this effect on ALL the text, but not just part of it. Here's what I have
let text = "\(20.4$) \(On Rush Hour)"
let attrStr = NSMutableAttributedString(string: text)
let range = (text as NSString).range(of: "20.4$")
attrStr.addAttribute(NSStrikethroughStyleAttributeName,
value: NSUnderlineStyle.styleSingle.rawValue,
range: range)
self.originalPriceLabel.attributedText = attrStr
Now, if I only replace
let range = (text as NSString).range(of: "20.4$")
with
let range = (text as NSString).range(of: text)
I see the strikethrough. What am I doing wrong?
Thanks!
Related
I have a textView and a String.
I would like to add this string with some attribute (Red color and bold font), but I would also like to keep the textView default font color to black if a user type in.
I'm trying to achieve this with NSAttributedString but since i dont know the text before this String i cant find a way to reach my goal. (I'm not gonna post some code since i'm only trying not working stuff with NSAttributedString)
Is there any way to do this?
let rangeMain = (textView!.text! as NSString).range(of: (textView!.text! as NSString) as String)
let rangeGreen = (textView!.text! as NSString).range(of: "a")
let rangeBlue = (textView!.text! as NSString).range(of: "b")
let rangeGray = (textView!.text! as NSString).range(of: "c")
let attributedString = NSMutableAttributedString(string: textView!.self.text!)
attributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.black, range: rangeMain)
attributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.blue, range: rangeBlue)
attributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.green, range: rangeGreen)
attributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.gray, range: rangeGray)
textView!.attributedText = attributedString
Did you try something like this?
It had to change the specific text (characters: a,b,c which you can rename you need to be)...
I'm having a certain sentence. I have to give black color to four words in that sentence.
This is how I have attempted that...
In viewDidLoad,
rangeArray = ["Knowledge","Events","Community","Offers"]
for text in rangeArray {
let range = (bottomTextLabel.text! as NSString).range(of: text)
let attribute = NSMutableAttributedString.init(string: bottomTextLabel.text!)
attribute.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.black , range: range)
self.bottomTextLabel.attributedText = attribute
}
But with this code, instead of having black color for all 4 words, I get only the word 'Offers' in black color. What am I doing wrong...?
In your code, you are updating the self.bottomTextLabel.attributedText for each run of the for loop.
Instead of that you must
create an NSMutableAttributedString using the sentence,
add all the relevant attributes as per your rangeArray and
then at last set that attrStr it as attributedText of bottomTextLabel.
Here is what I mean to say,
if let sentence = bottomTextLabel.text {
let rangeArray = ["Knowledge","Events","Community","Offers"]
let attrStr = NSMutableAttributedString(string: sentence)
rangeArray.forEach {
let range = (sentence as NSString).range(of: $0)
attrStr.addAttribute(.foregroundColor, value: UIColor.black, range: range)
}
self.bottomTextLabel.attributedText = attrStr
}
I have this function for adding a bullet to a textView
let bullet: String = " ● "
func setAttributedValueForBullets(bullet: String, positionWhereTheCursorIs: Int?) {
var textRange = selectedRange
let selectedText = NSMutableAttributedString(attributedString: attributedText)
if let line = positionWhereTheCursorIs {
textRange.location = line
}
selectedText.mutableString.replaceCharacters(in: textRange, with: bullet)
let paragraphStyle = createParagraphAttribute()
selectedText.addAttributes([NSParagraphStyleAttributeName: paragraphStyle], range: NSMakeRange(textRange.location, bullet.length))
self.attributedText = selectedText
self.selectedRange = textRange
}
and it works when inserting a bullet to a paragraph with just one line like this
but when I add it to a paragraph with more than one line this happen
I want it to look like in the first image, without that space in the bullet and the begining of text
I have also tried to use
selectedText.insert(bullet, at: textRange.location)
instead of
selectedText.addAttributes([NSParagraphStyleAttributeName: paragraphStyle], range: NSMakeRange(textRange.location, bullet.length))
This is essentially happening because of the space between the bullet point and the long description. If the description is too long, it will, by normal behavior, go on to it's own line, wrapping the rest of the string.
The easiest fix for this, is to use a non-breaking unicode space character (u00A0):
let bullet = "●\u{00A0}"
let string = "thisisaveryveryveryveryveryveryveryveryveryverylongstring"
textView.text = bullet + string
This will result in the expected behavior, where the very long string will not break into it's own line, as it will be connected to the preceding bullet point:
Also, if the space between the bullet and the string is too small, simply string together multiple non-breaking spaces:
let bullet = "●\u{00A0}\u{00A0}"
func attributeString() -> NSAttributedString{
let attribute = NSMutableAttributedString(string: "●DESCRIPTION\n", attributes: [NSFontAttributeName : UIFont.systemFont(ofSize: 14)])
let style = NSMutableParagraphStyle()
style.lineSpacing = 10
let range = NSMakeRange(0, attribute.string.characters.count)
attribute.addAttribute(NSParagraphStyleAttributeName, value: style, range: range)
return attribute
}
i've a single UIButton to set the text bold and unbold in UITextview
let range = mainTextView.selectedRange
let string = NSMutableAttributedString(attributedString: mainTextView.attributedText)
let attributes = [NSFontAttributeName: UIFont.boldSystemFontOfSize(12)]
string.addAttributes(attributes, range: mainTextView.selectedRange)
mainTextView.attributedText = string
My question is how to unbold the selected text if it is already bold? how to check if the selectedRange is already bold or not?
I have a UITableView and i would like to display the text of each row using different colors within the same line.
I've tried this code, trying to translate from Obj-C but i cannot have it working
let object = self.fetchedResultsController.objectAtIndexPath(indexPath) as NSManagedObject
var attrString: NSMutableAttributedString = NSMutableAttributedString(string: object.valueForKey("example1")!.description)
attrString.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSMakeRange(0, attrString.length))
var stringToCell:String = String(format: "%# %#", attrString, object.valueForKey("example2")!.description)
cell.textLabel?.text = stringToCell
The output of all this is
where the number 34 correspond to object.valueForKey("example1")!.description, so the problem is that the number is not red, and the second part (object.valueForKey("example2")!.description) is replaced by {.
If I leave this piece of code regarding NSAttributedString the row text is displayed correctly.
I think the problem might lie in assigning to cell.textLabel?.text instead of cell.textLabel?.attributedText. Perhaps something like this:
let object = self.fetchedResultsController.objectAtIndexPath(indexPath) as NSManagedObject
var attrString: NSMutableAttributedString = NSMutableAttributedString(string: object.valueForKey("example1")!.description)
attrString.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSMakeRange(0, attrString.length))
var descString: NSMutableAttributedString = NSMutableAttributedString(string: String(format: " %#", object.valueForKey("example2")!.description))
descString.addAttribute(NSForegroundColorAttributeName, value: UIColor.blackColor(), range: NSMakeRange(0, descString.length))
attrString.appendAttributedString(descString);
cell.textLabel?.attributedText = attrString
Wasn't sure if you wanted the second part of the string to be red or another color so I made it black.
If you can, avoid using NSMutableAttributedString and just pass in the attributes with the constructor:
private func PullToRefreshAttributedStringWithColor(color:UIColor) -> NSAttributedString {
return NSAttributedString(string: "Pull to Refresh", attributes: [
NSForegroundColorAttributeName:color
])
}
Here is a example of attribute text Swift 3
let mainText = "Here is a example of attributedString"
let attributeText = "attributedString"
let range = (mainText as NSString).range(of: attributeText)
let attributedString = NSMutableAttributedString(string:mainText)
attributedString.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.red, range: range)
attribute.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 14) , range: range)
lblTitle.attributedText = attributedString