line strikethrough is not show in middle of UILabel swift iOS - ios

I have assigned attributedText to UILabel see the following code, But strike through line is not displayed center (vertically/meddle) of UIlabel text.
let strikethroughAttribute = [
.font: UIFont(name: "Roboto-Medium", size: 16.0)!,
.foregroundColor: UIColor.gray,
.strikethroughStyle: NSUnderlineStyle.single.rawValue,
.strikethroughColor: UIColor.gray] as [NSAttributedString.Key : Any]
}
var formattedString = NSMutableAttributedString(string: "$198")
formattedString.addAttributes(strikethroughAttribute, range: NSMakeRange(0, formattedString.length))
label.attributedText = formattedString

Related

Rendering text in italics using core text in iOS

I want to render text in italics using core text. In UILabel, I was able to apply italics style(basically slanting the text) for a regular font by adding obliqueness attribute to the NSMutableAttributedString. Please refer the code and output below,
let attributes: [NSAttributedString.Key: Any] =
.font: UIFont(name: "YesevaOne-Regular", size: 20),
.obliqueness: 0.3
]
let label = "FontTest"
let attributedString = NSMutableAttributedString(string: label)
attributedString.addAttributes(attributes, range: NSRange.init(location: 0, length: (label as NSString).length))
fontestLabel.attributedText = attributedString
But, when i do the same in core text, the obliqueness attribute not getting applied to the text, whereas the other attributes like strokewidth, strokecolor was getting applied. I have added the code which i used and output below,
let attrString = NSMutableAttributedString(string: "Font Test")
let attributes: [NSAttributedString.Key: Any] = [
.backgroundColor : UIColor.white.cgColor,
.font : UIFont(name: "YesevaOne-Regular", size: 20)!,
.strokeWidth: -5,
.obliqueness: 0.3,
]
attrString.addAttributes(attributes, range: NSRange(location: 0, length: attrString.length))
let framesetter = CTFramesetterCreateWithAttributedString(attrString as CFAttributedString)
let frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, attrString.length), path, nil)
CTFrameDraw(frame, context)
Please help me, if anyone come across this situation?
PS: The custom font, which i used doesn't have italics ttf.

NSAttributedString: how to make multiline in another NSAttributedString

I am trying to create a multiline separate text in another text to achieve the below text style.
I have tried the below code to produce the goal but the third part of the code is creating a issue (with medium font)
private func createLimitedDetailText() -> NSAttributedString {
let totalText = "Attension, only\n 6 spaces\n left!"
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
let attributedString = NSMutableAttributedString(string: totalText, attributes: [
.font: FontFamily.OpenSans.light.font(size: 29.0),
.foregroundColor: UIColor.white,
])
let bigText = attributedString.addAttribute(.font, value: FontFamily.OpenSans.extrabold.font(size: 70), range: NSRange(location: 17, length: 1))
let medium = attributedString.addAttribute(.font, value: FontFamily.OpenSans.semibold.font(size: 29), range: NSRange(location: 18, length: 14))
let textRange = NSRange(location: 0, length: attributedString.length)
attributedString.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: textRange)
return attributedString
}
In case anyone is wondering the answer, below code generates the exact same result:
private func createLimitedDetailText() -> NSAttributedString {
let totalText = "Attension, only\n 6"
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineHeightMultiple = 0.90
paragraphStyle.alignment = .center
let attributedString = NSMutableAttributedString(string: totalText, attributes: [
.font: FontFamily.OpenSans.light.font(size: 29.0),
.foregroundColor: UIColor.white,
NSAttributedString.Key.paragraphStyle: paragraphStyle
])
attributedString.addAttribute(.font, value: FontFamily.OpenSans.extrabold.font(size: 70), range: NSRange(location: 17, length: 1))
let paragraphStyle2 = NSMutableParagraphStyle()
paragraphStyle2.lineHeightMultiple = 0.30
paragraphStyle2.alignment = .center
let attributedString2 = NSMutableAttributedString(string: " spaces\n left!", attributes: [
.font: FontFamily.OpenSans.semibold.font(size: 29.0),
.foregroundColor: UIColor.white,
.baselineOffset: 35,
.paragraphStyle: paragraphStyle2
])
attributedString.append(attributedString2)
return attributedString
}

Make half of the TextView's text color Different than other 50% of the text SWIFT

I've got a large text in my UITextView and I want to make the 50% of the text's color red and the other 50% green . I've added NSMutableAttributedString in the TextView but it works's for the full range of the text . How to divide the textView's text into two sections and color them into red and green ?
let strNumber: NSString = self.text as NSString // TextView Text
let range = (strNumber).range(of: strNumber as String)
let attribute = NSMutableAttributedString.init(string: strNumber as String)
attribute.addAttributes([NSAttributedString.Key.font : UIFont.systemFont(ofSize: 14) , NSAttributedString.Key.foregroundColor : UIColor.red], range: range)
self.attributedText = attribute
It seems you have an extension to UITextView. The following extension function will make the existing attributed text of a text view be half red and half green. All other existing attributes, if any, will remain.
extension UITextView {
func makeHalfRedGreen() {
if let text = self.text {
let half = text.count / 2
let halfIndex = text.index(text.startIndex, offsetBy: half)
let firstRange = NSRange(..<halfIndex, in: text)
let secondRange = NSRange(halfIndex..., in: text)
let attrTxt = NSMutableAttributedString(attributedString: attributedText)
attrTxt.addAttribute(.foregroundColor, value: UIColor.red, range: firstRange)
attrTxt.addAttribute(.foregroundColor, value: UIColor.green, range: secondRange)
attributedText = attrTxt
}
}
}
Try to use function like below
text_lbl.attributedText = self.decorateText(txt1: "Red Color", txt2: “Blue Color”)
func decorateText(txt1:String, txt2:String)->NSAttributedString{
let textAttributesOne = [NSAttributedStringKey.foregroundColor: UIColor.red, NSAttributedStringKey.font: UIFont(name: "Poppins-Regular", size: 12.0)!] as [NSAttributedStringKey : Any]
let textAttributesTwo = [NSAttributedStringKey.foregroundColor: UIColor.blue, NSAttributedStringKey.font: UIFont(name: "Poppins-SemiBold", size: 14.0)!] as [NSAttributedStringKey : Any]
let textPartOne = NSMutableAttributedString(string: txt1, attributes: textAttributesOne)
let textPartTwo = NSMutableAttributedString(string: txt2, attributes: textAttributesTwo)
let textCombination = NSMutableAttributedString()
textCombination.append(textPartOne)
textCombination.append(textPartTwo)
return textCombination
}

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

ios swift: Is it possible to change the font style of a certain word in a string?

I am extracting from a DB contents as strings. With a method I extract the longest word out of this string.
Now I would like to print out the entire string to a text label but would like to highlight the longest word in a different color and text style within the string.
How can I do that?
Do I need to cut the string into pieces - set the formatting - and put them all together again before giving it to the label?
Or is there any other (better) way?
If you already know the longest word you have to get the range of that word in the string. I prefer the NSString method rangeOfString: for this.
You then create a NSMutableAttributedString from the string, with your default attributes. Finally you apply highlighting attributes to the range you figured out earlier.
let longString = "Lorem ipsum dolor. VeryLongWord ipsum foobar"
let longestWord = "VeryLongWord"
let longestWordRange = (longString as NSString).rangeOfString(longestWord)
let attributedString = NSMutableAttributedString(string: longString, attributes: [NSFontAttributeName : UIFont.systemFontOfSize(20)])
attributedString.setAttributes([NSFontAttributeName : UIFont.boldSystemFontOfSize(20), NSForegroundColorAttributeName : UIColor.redColor()], range: longestWordRange)
label.attributedText = attributedString
Update for Swift 5.0
let longestWordRange = (longString as NSString).range(of: longestWord)
let attributedString = NSMutableAttributedString(string: longString, attributes: [NSAttributedStringKey.font : UIFont.systemFont(ofSize: 20)])
attributedString.setAttributes([NSAttributedStringKey.font : UIFont.boldSystemFont(ofSize: 20), NSAttributedStringKey.foregroundColor : UIColor.red], range: longestWordRange)
Which looks like this in my playground:
You want to look at Attributed Strings and NSRange. You can use both of these together to create different styles for ranges in the string. Here is a snippet:
myMutableString = NSMutableAttributedString(string: myString, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 18.0)!])
//Add more attributes here:
myMutableString.addAttribute(NSFontAttributeName, value: UIFont(name: "Chalkduster", size: 24.0), range: NSRange(location: 7,length: 5))
myMutableString.addAttribute(NSFontAttributeName, value: UIFont(name: "AmericanTypewriter-Bold", size: 18.0)!, range: NSRange(location:2,length:4))
myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSRange(location:2,length:4))
myMutableString.addAttribute(NSFontAttributeName, value: UIFont(name: "Georgia", size: 36.0)!, range: NSRange(location: 0, length: 1))
myMutableString.addAttribute(NSStrokeColorAttributeName, value: UIColor.blueColor(), range: NSRange(location: 0, length: 1))
myMutableString.addAttribute(NSStrokeWidthAttributeName, value: 2, range: NSRange(location: 0, length: 1))
myMutableString.addAttribute(NSBackgroundColorAttributeName, value: UIColor.greenColor(), range: NSRange(location: 0, length: myString.length))
myLabel.backgroundColor = UIColor.grayColor()
//Apply to the label
myLabel.attributedText = myMutableString
NSMutableAttributedString.
You create an NSMutableAttributedString and apply the effects you'd like with addAttributes:range.
Then assign it to the attributedText property of your UILabel.

Resources