Changing WKInterfaceLabel Text - swift

I currently have:
#IBOutlet var label: WKInterfaceLabel!
and
let myString = "Swift Attributed String"
let myAttribute = [ NSAttributedStringKey.foregroundColor: UIColor.blue ]
let myAttrString = NSAttributedString(string: myString, attributes: myAttribute)
label.setAttributedText(myAttrString)
Currently this code does not change the text of the label. Is there something obvious I am missing? Thanks for any help!

Your code is working perfectly fine but for only textcolor
You do not need to use attributed property you can directly use setTextColor() property.
label.setTextColor(UIColor.blue)
label.setText(myString)
You can refer WKInterfaceLabel and setTextColor

Related

How can set accessibility identifier in attributedPlaceholder

I have the given piece of code
textField.attributedPlaceholder = NSAttributedString(string: Strings.test,
attributes: [NSAttributedString.Key.foregroundColor: ColorTheme.test])
How I can set an accessibility identifier in attributedPlaceholder so that I can later use the containing string in the UITest?
You need to give accessibility identifier to your textfield. Then you can get the placeholder text via property.
let textField = XCUIApplication().textFields["textFieldIdentifier"]
let placeholderText = textField.placeholderValue

best way to customize uilabel and make it internationalizable

I'm changing my app to make it localizable/internationalizable, by adding multiple languages, so I have some texts on the storyboard and others in the swift files, so I have to make them internationalizable, but I don't know how to keep a customized font and make it internationalizable.
I have created these:
So if I understand the first strings files are for UILabel which are written in the storyboard. The second is for text in swift files also UIlabel linked in.
But I have to customize, fonts, colours... for some texts, and I don't know how to do for the UILabel in the storyboard, but for swift files I did it:
var main_string = "VOTRE EXPERT DU\nREGROUPEMENT DE CRÉDITS\nEN FRANCE DEPUIS 1998."
var string_to_color = "1998"
var string_to_bold = "REGROUPEMENT DE CRÉDITS"
var range_color = (main_string as NSString).range(of: string_to_color)
var range_bold = (main_string as NSString).range(of: string_to_bold)
let attributedString = [ NSAttributedStringKey.font: UIFont(name: "gotham-book", size: 18.0)! ]
let myString = NSMutableAttributedString(string: "VOTRE EXPERT DU\nREGROUPEMENT DE CRÉDITS\nEN FRANCE DEPUIS 1998.", attributes: attributedString)
NSLocalizedString("Welcome", comment: "")
myString.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.orange , range: range_color)
let font = UIFont(name: "gotham-bold", size: 18.0)
myString.addAttribute(NSAttributedStringKey.font, value: font, range: range_bold)
accueilTopLabel.attributedText = myString
But how can I manage different languages with? Because I use ranges so it looks difficult. What the best way to manage it?

How to change CenterText Font in PieChart in ios-charts?

I would like to change the Center Text Font and Font Size of a PieChart. I would like to make it as big as it's possible inside the center circle. Is there any on-board feature that helps me to change the font and font size or do i have to overwrite some Framework classes?
Didn't find any useful information in the documentation :(
That's my basic Chart Formating method:
func setAttributes(view: PieChartView){
view.legend.enabled = true
view.descriptionText = ""
view.userInteractionEnabled = false
view.drawSliceTextEnabled = false
view.animate(xAxisDuration: 2.0, yAxisDuration: 2.0, easingOption: ChartEasingOption.EaseInOutBack)
}
Regards!
Swift 3
You can use this code to change font in swift 3.
let myAttribute = [ NSFontAttributeName: UIFont(name: "IranSansMobile", size: 15.0)! ]
let myAttrString = NSAttributedString(string: "My String", attributes: myAttribute)
chart.centerAttributedText = myAttrString
The centered text in pie chart is called centerAttributedText, in PieChartView. It's NSAttributedText, so you can define many custom attributes.
You can simply change its font and size like below:
centerText = #"Whatever you like";
[centerText setAttributes:#{
NSFontAttributeName: [UIFont fontWithName:#"HelveticaNeue-Light" size:12.f],
NSParagraphStyleAttributeName: paragraphStyle
} range:NSMakeRange(0, centerText.length)];
pieChartView.centerAttributedText = centerText;
It's in Objective-C, but should be easy for you to translate into swift, since you only need to care about the attributes
swift 5
var pieChartView = PieChartView()
...
let myAttrString = NSAttributedString(string: "My String", attributes: nil)
pieChartView.centerAttributedText = myAttrString

how to make text bold for detailedTextLabel in swift

cell!.textLabel?.text = vehicle["vrn"].string
cell?.detailTextLabel?.text = stateString
I want to display stateString as bold and also tried to use textLabel instead of detailedText but it did not work.
You can set the font property of the detailTextLabellike so:
cell.detailTextLabel?.font = UIFont.boldSystemFontOfSize(15.0)
You can use the font property inside the UILabel class.
// You need to set the name of your font here
cell.detailTextLabel?.font = UIFont(name:"HelveticaNeue-Bold", size: 16.0)
There are other options using the attributedText
property but implies a little more of code, something like this:
// Define attributes to set
let labelFont = UIFont(name: "HelveticaNeue-Bold", size: 16)
let attributes :Dictionary = [NSFontAttributeName : labelFont]
// Create the attributed string
var attrString = NSAttributedString(string: "textOfYourLabel", attributes:attributes)
cell.detailTextLabel?.attributedText = attrString
I hope this help you.
If you want to bold text label using story board, simply select SHADOW colour in attributes inspector to same of that Text colour. Its works for me try it....

Underline text in a UITextView

How can I underline text in a UITextView. I understand that I would need to create a subclass of UITextView, but what would go under drawRect:?
Thanks.
Try to use NSAttributedString as follows and set in UITextView. This works for iOS6.
NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:#"Some String"];
[attString addAttribute:(NSString*)kCTUnderlineStyleAttributeName
value:[NSNumber numberWithInt:kCTUnderlineStyleSingle]
range:(NSRange){0,[attString length]}];
For more info on NSAttributedString check this How do you use NSAttributedString?
For eg:-
textView.attributedText = attString;
From apple documentation on UITextView,
In iOS 6 and later, this class supports multiple text styles through
use of the attributedText property. (Styled text is not supported in
earlier versions of iOS.) Setting a value for this property causes the
text view to use the style information provided in the attributed
string. You can still use the font, textColor, and textAlignment
properties to set style attributes, but those properties apply to all
of the text in the text view.
attributedText:
The styled text displayed by the text view.
#property(nonatomic,copy) NSAttributedString *attributedText
Discussion: This property is nil by default. Assigning a new value to this property also replaces the value of the text property with the same string data, albeit without any formatting information. In addition, assigning a new a value updates the values in the font, textColor, and textAlignment properties so that they reflect the style information starting at location 0 in the attributed string.
If you want to avoid having to include CoreText, you can utilize an attributed string with this attribute:
#{NSUnderlineStyleAttributeName: #(NSUnderlineStyleSingle)}
If this is static text, you can underline it in Interface Builder. Make sure to make the text 'Attributed' first by selecting 'Attributed' in the drop down menu:
textViewMessage.linkTextAttributes = #{NSForegroundColorAttributeName: [UIColor blueColor], NSUnderlineStyleAttributeName: [NSNumber numberWithInt:NSUnderlineStyleSingle]};
If you want to format your text (with underlined words, links, colored words...) I suggest you to use FTCoreText
-(IBAction)underline:(id)sender
{
NSDictionary *underlineAttribute = #{NSUnderlineStyleAttributeName: #(NSUnderlineStyleSingle)};
texts.attributedText = [[NSAttributedString alloc] initWithString:texts.text
attributes:underlineAttribute];
}
You can't use "kCTUnderlineStyleAttributeName" or "kCTUnderlineStyleSingle"
Now you must do it like this:
NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:#"Text"];
[attString addAttribute:NSUnderlineStyleAttributeName
value:#(NSUnderlineStyleSingle)
range:(NSRange){0,[attString length]}];
If you are using iOS 6 then you can use the attributedText attribute of UITextView. Apply underline formatting to the text. You can also set the typingAttributes property to ensure the text that the user types has a specific set of formatting if you wish.
I recommend you to use CoreText. A Basic tutorial is here on raywenderlich.
I recommend you to use MFUnderlinedTextView, it will be helpful.
This is how I did it using Swift 5:
let attributedString = NSMutableAttributedString(string: myTextView.text ?? "")
myTextView.linkTextAttributes = [NSAttributedString.Key(rawValue: NSAttributedString.Key.underlineStyle.rawValue): NSUnderlineStyle.single.rawValue] as [NSAttributedString.Key: Any]?
myTextView.attributedText = attributedString
Swift 5.
As my UITextView if for inserting text, I created an extension function as bellow.
extension UITextView {
func underlined() {
let border = CALayer()
let width = CGFloat(1.0)
border.borderColor = UIColor.lightGray.cgColor
border.frame = CGRect(x: 0, y: self.frame.size.height - 5, width: self.frame.size.width, height: 1)
border.borderWidth = width
self.layer.addSublayer(border)
self.layer.masksToBounds = true
let style = NSMutableParagraphStyle()
style.lineSpacing = 15
let attributes = [NSAttributedString.Key.paragraphStyle : style, NSAttributedString.Key.foregroundColor : UIColor.darkGray, NSAttributedString.Key.font : UIFont.systemFont(ofSize: 13)]
self.attributedText = NSAttributedString(string: self.text, attributes: attributes)
}
}
The border is drawling the line and the style is adding the spacing between the lines.
Usage in your UIView custom layout:
override func layoutSubviews() {
super.layoutSubviews()
self.dateAndTimeInput.underlined()
}
Image with the result
let someString = NSMutableAttributedString(string: "Your String", attributes: [NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 20), NSAttributedString.Key.underlineStyle : NSUnderlineStyle.single.rawValue])
someStringTextView.attributedText = titleAT
U can just give your string a bunch of attributes like bold, underlined etc.
To underline a text, you have to go where you can select copy, cut , delete options there are now more options like B/I/U( bold, italic, underline). Choose this option and this is it. And to unable it, choose underline option again.