How do I add an outline to my label in Swift? - ios

I'm new to Stackoverflow.
I am currently developing a mobile application using XCode for iOS.
However I'm trying to set add a white outline/stroke to my label but I do not know hot to. I have tried searching these forums but could not find any solution in swift.
I have tried using the shadow property, but it's not satisfactory.
How do I add an outline to my label in Swift?
Edit: The text should look like the text in this picture: http://cf.chucklesnetwork.com/items/7/5/7/4/4/original/yo-dawg-i-heard-you-like-captions-so-i-put-captions-of-captions.jpg

You need to use NSAttributedString, set strokeColor and strokeWidth to set outline and foregroundColor to set text color. Try this:
let attrString = NSAttributedString(
string: "Write Something with Outline",
attributes: [
NSAttributedStringKey.strokeColor: UIColor.black,
NSAttributedStringKey.foregroundColor: UIColor.white,
NSAttributedStringKey.strokeWidth: -2.0,
NSAttributedStringKey.font: UIFont.systemFont(ofSize: 17.0)
]
)
yourLabel.attributedText = attrString
This will look like below:

You can use this OutlinedLabel. Unlike most examples, it actually outlines the text.
And you can even gradient the outline.
Just set the outline color and the line width:
label.outlineColor = .white
label.outlineWidth = 7
This will look like this

Related

How to make Label first line text less from other line

I want something like this, first line text should be wrap according to rating button width and rating button width may be less or greater.
If anyone have idea please give your answer.
AFAIK UILabel doesn't have such feature but you can achieve this behaviour by using UITextView. You can tweak it to look like your label and then set exclusion path for its' text container like this:
let exclusionPath = UIBezierPath(rect: textView.convert(button.frame, from: button.superview))
textView.textContainer.exclusionPaths = [exclusionPath]
If you also want to change button width according to the available width in the first line, then you can set excluded path frame with minimal width (according to text width and insets in button) and than calculate available space in first row similar to the way mentioned in this answer (it shows how to calculate the width of the last line but doing the same for the first one is quite similar)
Just try this codes, it will help you
myMutableString = NSMutableAttributedString(string: myString, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 18.0)!])
myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSRange(location:2,length:4))
myMutableString.addAttribute(NSFontAttributeName, value: UIFont(name: "SFUIText-Bold", size: 14)!, range: NSRange(location:2,length:4))
labName.attributedText = myMutableString

Red Asterisk directly beside placeholder in input box in ios?

I would like to put a red asterix( * ) besides placeholder in ios in the registration form where i am using the uitextfield. I know how to change the placeholder color but in my requirement i have the placeholder color as grey with red asterix( * ) following it. How to achieve this in swift 3? Is it possible to set it in storyboard itself?
You can use the built-in, attributedPlaceholder property of UITextField.
Like this:
let passwordAttriburedString = NSMutableAttributedString(string: "Password")
let asterix = NSAttributedString(string: "*", attributes: [.foregroundColor: UIColor.red])
passwordAttriburedString.append(asterix)
self.textField.attributedPlaceholder = passwordAttriburedString

What is UTF-8 character for "previous arrow", same as system back in UINavigationItem?

I just couldn't find utf-8 encoding for character like following:
I think that it is quite possible to create this in code, as a composed character from two dashes: / and \, but I do not know what characters to compose.
Maybe U+003C
print("\u{003C}") // <
Possibly U+2039.
print("\u{2039}") // ‹
Or U+276E.
print("\u{276E}") // ❮
I'm not certain if this will work for a navigation item, but you could make use of NSAttributedString to have string with different sizes of the substrings within it. This is described in detail in this great answer:
How do I make an attributed string using Swift?
As an example for your case:
let myAttributeBack = [ NSFontAttributeName: UIFont(name: "HelveticaNeue-UltraLight", size: 30.0)!]
let attrString = NSMutableAttributedString(string: "\u{276E} ", attributes: myAttributeBack)
let myAttributeText = [ NSFontAttributeName: UIFont(name: "HelveticaNeue-UltraLight", size: 15.0)!]
let backString = NSAttributedString(string: "back", attributes: myAttributeText)
attrString.appendAttributedString(backString)
myLabel.attributedText = attrString
However, as you write below, perhaps it's not possible to use attributed strings for title of a navigation bar item. Then I'd assume that the navigation bar example you showed above simply contains an image with the back bracket and a string "back" for the title text.
Is it this:
〈
The code is U+276C.
see https://en.wiktionary.org/wiki/%E3%80%88

Way stroke outline number using swift

is there anyway to draw or stroke a number using swift for iOS? The number has to be in the outline style. Thanks in advance.
Use an NSAttributedString. UITextField can display attributed strings using the attributedText property.
let number = 5 // Whatever number you are looking to output
let font = UIFont.systemFontOfSize(12.0)
let attribs = [
NSForegroundColorAttributeName: UIColor.whiteColor(),
NSStrokeColorAttributeName: UIColor.blackColor(),
NSFontAttributeName: font,
NSStrokeWidthAttributeName: 1.0
]
let outlinedNumber = NSAttributedString(string: "\(number)", attributes: attribs)
Play with the various attributes to get the effect you want.

How to add outline to text in a UITextView?

I'm a newcomer to text programming in iOS and a bit stumped at the moment. I am creating an example project to learn how to make a lolcat app (start with something fun!).
I have a UIImage with the cat and I'm putting some text on top of it by having a UITextView on top with text.
I'd like to end up with something like this for the text:
I was able to successfully pull this off when I had the text in a UILabel. I used code I found on SO that used drawTextInRect to accomplish the magic.
Now, I'd like to do the same thing in the UITextView (I want to be able to scroll the text).
**EDIT: ** so here is what I'm trying so far:
var foo = NSAttributedString(string: text, attributes: [
NSStrokeColorAttributeName : UIColor.blackColor(),
NSForegroundColorAttributeName : UIColor.whiteColor(),
NSStrokeWidthAttributeName : NSNumber(float: -4.0),
NSFontAttributeName : UIFont.systemFontOfSize(30.0)
])
That gets me a result like this:
If I now double the stroke width to -8.0 I get the below result which looks pretty darn bad in my estimation. It seems like there is a tradeoff between the stroke width and how much of the fill I can see. Ideally I'd like the stroke to just get wider without eating up the white text.
You need to read up on NSAttributedString. You can give letters an outline stroke:
https://developer.apple.com/library/ios/documentation/UIKit/Reference/NSAttributedString_UIKit_Additions/#//apple_ref/doc/constant_group/Character_Attributes
Use the NSStrokeColorAttributeName and NSStrokeWidthAttributeName to configure the outline stroke.
Once you have your attributed string you can put it an a label, put it in a text view, or draw it directly, as you please.
Might look a little better if you add some expansion and a tiny bit of shadow:
You can use the help of library DrawingLabel without any CoreGraphics code
DrawingStroke *blackStroke = [DrawingStroke new];
blackStroke.strokeWidth = 3.0;
blackStroke.strokeColor = [UIColor blackColor];

Resources