NSAttributedString: heading on non-paragraphs - ios

I'm building a dictionary with a big UITextView with multiple NSAttributedString for each word. I'm trying to set a fixed space before lines like below but I can't set heading on the text starting by "L'ensemble" as it's not a paragraph (not starting with \n ).
Do you have an idea on to achieve this?
Here is my code so far which doesn't work as valueParaStyle doesn't do anything because valueTextdoesn't start with \n.
Thanks.
let senseNumberParaStyle = NSMutableParagraphStyle()
senseNumberParaStyle.paragraphSpacingBefore = 20
let valueParaStyle = NSMutableParagraphStyle()
valueParaStyle.firstLineHeadIndent = 20
valueParaStyle.headIndent = 20
for i in 0..<senses.count {
let senseNumberText = NSAttributedString(string: "\n\(i + 1).", attributes: [
NSFontAttributeName: UIFont(name: "AvenirNext-Bold", size: 14)!,
NSForegroundColorAttributeName: UIColor(red: 1, green: 0.275, blue: 0.294, alpha: 1),
NSParagraphStyleAttributeName: senseNumberParaStyle
])
wordText.appendAttributedString(senseNumberText)
if let value = senses[i].value {
let valueText = NSAttributedString(string: " \(value)", attributes: [
NSFontAttributeName: UIFont(name: "AvenirNext-Regular", size: 15)!,
NSForegroundColorAttributeName: UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: 1),
NSParagraphStyleAttributeName: valueParaStyle
])
wordText.appendAttributedString(valueText)
}
}

You are on the right track. You just need to add a "\t" at your first line:
var paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.headIndent = 20
label.attributedText = NSAttributedString(string: "1.\tHere comes your text. Indent as you really want it", attributes:[NSParagraphStyleAttributeName : paragraphStyle])
Without the "\t":
With "\t":

Related

How to properly apply stroke for non-latin characters in Swift?

I'm working on a project to apply text attributes to UIlabel text. I'm using the common NSAttributedString keys to basically apply strokeColor and strokeWidth and foregroundColor to generate the desired outlining effect. The problem appears on non-latin characters, such as Arabic, where letters are individual highlighted instead of the entire work. The letter typically are connected in Arabic, unlink english were letters are spaced. I'm attaching the example I'm working on with a screenshot of the issue and the desired outcome. I would appreciate your support and suggestions.
let quote = "الكتابة على الصور بخطوط جميلة"
//let font = UIFont.systemFon.selft(ofSize: 50)
let attributes: [NSAttributedString.Key: Any] = [
.strokeColor: UIColor.green,
.strokeWidth: -3.0,
.foregroundColor: UIColor.red,
.font: UIFont(name: "Georgia-Bold", size: 40)!,
]
let attributedQuote = NSAttributedString(string: quote, attributes: attributes)
TextLabel.attributedText = attributedQuote
outcome with issue:
desired outcome:
Taking a quick look at this - and comparing it to similar output from css - I think you'll need to use two labels layers on top of each other.
Give this a try:
let backLabel = UILabel()
let frontLabel = UILabel()
backLabel.translatesAutoresizingMaskIntoConstraints = false
frontLabel.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(backLabel)
view.addSubview(frontLabel)
NSLayoutConstraint.activate([
backLabel.topAnchor.constraint(equalTo: view.topAnchor, constant: 100.0),
backLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20.0),
backLabel.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20.0),
frontLabel.topAnchor.constraint(equalTo: backLabel.topAnchor),
frontLabel.leadingAnchor.constraint(equalTo: backLabel.leadingAnchor),
frontLabel.trailingAnchor.constraint(equalTo: backLabel.trailingAnchor),
])
let quote = "الكتابة على الصور بخطوط جميلة"
var attributes: [NSAttributedString.Key: Any] = [
.strokeColor: UIColor.green,
.strokeWidth: 12.0,
.foregroundColor: UIColor.red,
.font: UIFont(name: "Georgia-Bold", size: 40)!,
]
var attributedQuote = NSAttributedString(string: quote, attributes: attributes)
backLabel.attributedText = attributedQuote
attributes = [
.strokeColor: UIColor.green,
.strokeWidth: 0,
.foregroundColor: UIColor.red,
.font: UIFont(name: "Georgia-Bold", size: 40)!,
]
attributedQuote = NSAttributedString(string: quote, attributes: attributes)
frontLabel.attributedText = attributedQuote
Result:

TTTAttributedLabel, change of color works only the first time controller launches in Swift

self.descriptionLbl.text = actionReqdText
let labelString = self.descriptionLbl.text as! NSString
let contactRange1 = labelString.range(of: actionString1)
let contactRange2 = labelString.range(of: actionString2)
self.descriptionLbl.addLink(to: URL(string: link1), with: contactRange1)
self.descriptionLbl.addLink(to: URL(string: link2), with: contactRange2)
let attributedText = NSMutableAttributedString(attributedString: descriptionLbl.attributedText!)
if UIScreen.main.bounds.size.height < 900 {
attributedText.addAttributes([NSAttributedStringKey.font : UIFont(name: HELVETICA_LIGHT, size: 13)!], range: contactRange1)
attributedText.addAttributes([NSAttributedStringKey.font : UIFont(name: HELVETICA_LIGHT, size: 13)!], range: contactRange2)
} else {
attributedText.addAttributes([NSAttributedStringKey.font : UIFont(name: HELVETICA_LIGHT, size: 18)!], range: contactRange1)
attributedText.addAttributes([NSAttributedStringKey.font : UIFont(name: HELVETICA_LIGHT, size: 18)!], range: contactRange2)
}
attributedText.addAttributes([NSAttributedString.Key(rawValue: kCTForegroundColorAttributeName as String as String): UIColor(red: 0.06, green: 0.49, blue: 0.25, alpha: 1.0)], range: contactRange1)
attributedText.addAttributes([NSAttributedString.Key(rawValue: kCTForegroundColorAttributeName as String as String): UIColor(red: 0.06, green: 0.49, blue: 0.25, alpha: 1.0)], range: contactRange2)
attributedText.addAttributes([NSAttributedStringKey.underlineStyle: NSUnderlineStyle.styleNone.rawValue], range: contactRange1)
attributedText.addAttributes([NSAttributedStringKey.underlineStyle: NSUnderlineStyle.styleNone.rawValue], range: contactRange2)
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 1.1
attributedText.addAttribute(NSAttributedStringKey.paragraphStyle, value:paragraphStyle, range:NSMakeRange(0, attributedText.length))
descriptionLbl.attributedText = attributedText
I've been trying this code to get a text in green color and with no underline style. It works the first time I launch the view controller. The next time I get to this screen, it is in native blue and underlined. Any help will be appreciated.
self.descriptionLbl.text = actionReqdText
let labelString = self.descriptionLbl.text as! NSString
let contactRange1 = labelString.range(of: actionString1)
let contactRange2 = labelString.range(of: actionString2)
let attributedText = NSMutableAttributedString(attributedString: descriptionLbl.attributedText!)
var linkAttributes: [AnyHashable : Any] = [:]
linkAttributes[NSAttributedString.Key.underlineStyle] = NSUnderlineStyle.styleNone.rawValue
if UIScreen.main.bounds.size.height < 900 {
attributedText.addAttributes([NSAttributedStringKey.font : UIFont(name: HELVETICA_LIGHT, size: 13)!], range: contactRange1)
attributedText.addAttributes([NSAttributedStringKey.font : UIFont(name: HELVETICA_LIGHT, size: 13)!], range: contactRange2)
} else {
attributedText.addAttributes([NSAttributedStringKey.font : UIFont(name: HELVETICA_LIGHT, size: 18)!], range: contactRange1)
attributedText.addAttributes([NSAttributedStringKey.font : UIFont(name: HELVETICA_LIGHT, size: 18)!], range: contactRange2)
}
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 1.1
linkAttributes[NSAttributedString.Key.foregroundColor] = UIColor(red: 0.06, green: 0.49, blue: 0.25, alpha: 1.0)
linkAttributes[NSAttributedString.Key.paragraphStyle] = paragraphStyle
self.descriptionLbl.attributedText = attributedText
self.descriptionLbl.linkAttributes = linkAttributes
self.descriptionLbl.addLink(to: URL(string: link1), with: contactRange1)
self.descriptionLbl.addLink(to: URL(string: link2), with: contactRange2)
Implemented the above code by replacing the addAttributes with link attributes and moving the addLinks after adding the attributes. This resolved the issue

Xcode 8.3.3 - How can I make 2 rows in a label?

So basically, I have created a speedometer but right now I have it outputting "30km/h" on one line.
What I want it to look like is have 30 on one line and km/h below it.
And if anyone knows how to make that thicker line that goes up the faster you go, that would be a great help.
Here is what my code looks like right now:
let speed = (location.speed*3.6)
let speedInt: Int = Int(speed)
//statusLabel.backgroundColor = UIColor.red
//statusLabel.layer.cornerRadius = 10.0
//statusLabel.clipsToBounds = true
let statusLabel = UILabel()
let size:CGFloat = 70.0
statusLabel.textColor = UIColor.black
statusLabel.textAlignment = .center
statusLabel.font = UIFont.systemFont(ofSize: 13.0)
statusLabel.frame = CGRect(x : 172.0,y : 580.0,width : size, height : size)
statusLabel.layer.cornerRadius = size / 2
statusLabel.layer.borderWidth = 2.0
statusLabel.layer.backgroundColor = UIColor.white.cgColor
statusLabel.layer.borderColor = UIColor.init(colorLiteralRed: 14.0/255, green: 122.0/255, blue: 254.0/255, alpha: 1.0).cgColor
statusLabel.text = "\(speedInt) km/h"
You should use UILabel's attributedText property in order to achieve this look.
Use \n to insert a new line to the label.
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
let bigAttr = [NSForegroundColorAttributeName: UIColor.white,
NSFontAttributeName: UIFont.systemFont(ofSize: 60, weight: UIFontWeightLight),
NSParagraphStyleAttributeName: paragraphStyle]
let smallAttr = [NSFontAttributeName: UIFont.systemFont(ofSize: 12, weight: UIFontWeightLight)]
let attributedString = NSMutableAttributedString(string: "\(speedInt)", attributes: bigAttr)
attributedString.append(NSAttributedString(string: "\nkm/h", attributes: smallAttr))
statusLabel.attributedText = attributedString
Add a \n where you want to split the string. "30 \n kmh"
Or simply add two labels.
You can simply add 2 lines in label with the help of UI. Just take a look at below snap
as shown above you can set the line break by word wrapping and simply add the title whole you want. in your case 0 km/h.
Then put the cursor at k and press ctrl + Enter. The title will appears in two lines.

How to color potion of an UILabel in swift 3

I want to achieve something like this without using 2 labels.
How can I create an UILabel like this.Please help me.
You can use the NSBackgroundColorAttributeName property of the NSMutableAttributedString to achieve the above result
var attributedString = NSMutableAttributedString(string:yourString)attributedString.addAttribute(NSBackgroundColorAttributeName, value: .redColor() , range: range)
yourLabel.attributedText = attributedString
For achieving this output we use NSAttribute class
Try below code
//This is for setting border of UILabel
let labelColor = UIColor(colorLiteralRed: 255.0/255.0, green: 115.0/255.0, blue: 116.0/255.0, alpha: 1.0)
lblNoOfDays.layer.masksToBounds = true
lblNoOfDays.layer.borderWidth = 1.0
lblNoOfDays.layer.borderColor = labelColor.cgColor
//This is value we display
let strValueTitle = "No of Days"
let strValue = "04"
//Attribute dictionary we use
//NSFontAttributeName:- sets font and it's size
//NSForegroundColorAttributeName:- sets text colour
//NSBackgroundColorAttributeName:- sets background color
let attributeTitle = [NSFontAttributeName: UIFont.systemFont(ofSize: 11.0), NSForegroundColorAttributeName: labelColor, NSBackgroundColorAttributeName: UIColor.clear]
let attributeVal = [NSFontAttributeName: UIFont.systemFont(ofSize: 12.0), NSForegroundColorAttributeName: UIColor.white, NSBackgroundColorAttributeName: labelColor]
//Implement attributes to string
let attributTitleString = NSMutableAttributedString(string: strValueTitle, attributes: attributeTitle)
let attributeValue = NSAttributedString(string: strValue, attributes: attributeVal)
//Append attributed string
attributTitleString.append(attributeValue)
//Assign attributed string to label
lblNoOfDays.attributedText = attributTitleString
lblNoOfDays.sizeToFit()
You need to set padding in UILabel for getting desired output.

why multiline attributedString UITextView has a different line height?

I get different line height in textView with using same font
How to set fixed line height?
I have done a lot of attempts, any help is appreciated, thanks
set NSMutableParagraphStyle lineSpacing is useless
set lineHeightMultiple is to make the difference more obvious
[
demo
import UIKit
import PlaygroundSupport
let view = UIView(frame: CGRect(x: 0, y: 0, width: 400, height: 1000))
let data: [String] = [
"商品名称: 巧克力",
"商品名称: 巧克力",
"商品名称: 巧克力",
"注册未成功,请验证电子邮件",
"注册未成功,请验证电子邮件",
"注册未成功,请验证电子邮件",
"测试文字, 测试文字,测试文字",
"测试文字, 测试文字,测试文字",
"测试文字, 测试文字,测试文字",
]
let textView = UITextView(frame: view.frame)
let paragraphStyle = NSMutableParagraphStyle()
let bodyFont = UIFont.systemFont(ofSize: 20.0)
paragraphStyle.lineHeightMultiple = 4
var stripe = false
// attributedString
let mutableAttributedString = NSMutableAttributedString(string: "Test TextViewAttributedString\n", attributes: [
NSFontAttributeName: UIFont.systemFont(ofSize: 18.0)
])
for text: String in data {
var backgroundColor = UIColor(red:0.13, green:0.38, blue:0.95, alpha:1.00)
if stripe {
backgroundColor = UIColor(red:0.92, green:0.12, blue:0.38, alpha:1.00)
}
let contentAttributedString = NSAttributedString(string: text, attributes: [
NSBackgroundColorAttributeName: backgroundColor,
NSParagraphStyleAttributeName: paragraphStyle,
NSFontAttributeName: bodyFont
])
mutableAttributedString.append(contentAttributedString)
stripe = !stripe
// add newline character
let newlineAttributedString = NSAttributedString(string: "\n")
mutableAttributedString.append(newlineAttributedString)
}
textView.attributedText = mutableAttributedString
view.addSubview(textView)
PlaygroundPage.current.liveView = view
I found the reason, the newlineAttributedString also need NSFontAttributeName
let newlineAttributedString = NSAttributedString(string: "\n", attributes: [
NSFontAttributeName: bodyFont
])
mutableAttributedString.appendAttributedString(newlineAttributedString)

Resources