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.
Related
I'm trying to implement by-design labels coming from Sketch e.g. I need text styles with font size = 19 and line height = 50. So I ended up using NSAttributedString with NSMutableParagraphStyle but was stopped by problem with text being sticked to bottom of UILabel
I've already tried to use lineHeightMultiple and lineSpacing but those didn't give me the line height I wanted so I ended up using minimumLineHeight and maximumLineHeight equal the same
Here is my approach to make NSAttributedString
private static func makeAttributedString(
with attributes: TextAttributes,
text: String? = nil,
alignment: NSTextAlignment = .center
) -> NSAttributedString {
let font = UIFont(name: attributes.font.rawValue, size: attributes.fontSize)!
let paragraph = NSMutableParagraphStyle()
paragraph.alignment = alignment
paragraph.paragraphSpacing = attributes.paragraph
paragraph.minimumLineHeight = attributes.lineHeight // equal 50 in my case
paragraph.maximumLineHeight = attributes.lineHeight // equal 50 in my case
let attributes: [NSAttributedStringKey: Any] = [
NSAttributedStringKey.paragraphStyle: paragraph,
NSAttributedStringKey.foregroundColor: attributes.textColor,
NSAttributedStringKey.kern: attributes.kern,
NSAttributedStringKey.font: font
]
return NSAttributedString(string: text ?? "", attributes: attributes)
}
I expect result similar to design
but actually getting
Note: setting height constraint to 50 is not applicable because I also need multiline labels but there is the same bug with them
Seems like I've found some workaround myself, maybe it will help someone.
The method is about setting baselineOffset like this:
NSAttributedStringKey.baselineOffset: (attributes.lineHeight - font.lineHeight) / 4
Works like charm:
https://i.imgur.com/a2EOf5R.png
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
I am trying to put some text on top of an image that would be in the center of a CGRect which would be on top of that image. Here is a bit of code:
let textColor: UIColor = UIColor.whiteColor()
let textFont: UIFont = UIFont(name: "Helvetica Bold", size: 17
)!
let textAlignment = NSTextAlignment.Center
let textFontAttributes = [
NSFontAttributeName: textFont,
NSForegroundColorAttributeName: textColor,
NSTextAlignment: textAlignment
]
myString.drawInRect(rect, withAttributes: textFontAttributes)
The problem is in the text alignment. When I write it like this I get an error:
Type of expression is ambiguous without more context
When I set key and value types to [String : AnyObject] the compiler complains again:
Cannot convert value of type 'NSTextAlignment.Type' to expected dictionary key type 'String'
Which I understand. I researched this for like two hours and haven't found any up to date solution and not single one how cloud one write this using Swift.
NSTextAlignment isn't a valid key. Note how the other keys end in Name. See the docs for NSFontAttributeName and NSForegroundColorAttributeName to see the list of valid keys.
Instead of NSTextAlignment you need to use NSParagraphStyleAttributeName which requires that you create an instance of NSParagraphStyle. That is where you set the alignment to .Center.
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .Center
The text alignment belongs to the paragraph style. Create a NSMutableParagraphStyle instance and pass it with key NSParagraphStyleAttributeName to the attributes.
let style = NSMutableParagraphStyle()
style.alignment = .Center
let textFontAttributes = [
NSFontAttributeName: textFont,
NSForegroundColorAttributeName: textColor,
NSParagraphStyleAttributeName: style
]
I am running into a strange issue while working on developing a Meme app. I am trying to create a UITextField that has a black outline with filled white text. I am able to create just a white text field and another that is outlined in black, however I cannot get both simultaneously. Any help would be much appreciated.
Here is the attributes I am assigning to the text field:
let attribs = [
NSForegroundColorAttributeName: UIColor.whiteColor(),
NSFontAttributeName: UIFont(name: "HelveticaNeue-CondensedBlack", size: 40)!,
NSStrokeColorAttributeName: UIColor.blackColor(),
NSStrokeWidthAttributeName: 1.0
]
Here is what the app looks like:
Apple has a page describing exactly the problem you were facing: Drawing attributed strings that are both filled and stroked. The trick is to change the stroke width to negative:
Swift 4:
let attributes: [NSAttributedStringKey: Any] = [
.foregroundColor: UIColor.white,
.font: UIFont(name: "HelveticaNeue-CondensedBlack", size: 40)!,
.strokeColor: UIColor.black,
.strokeWidth: -1 // Change here
]
Swift 2:
let attributes = [
NSForegroundColorAttributeName: UIColor.whiteColor(),
NSFontAttributeName: UIFont(name: "HelveticaNeue-CondensedBlack", size: 40)!,
NSStrokeColorAttributeName: UIColor.blackColor(),
NSStrokeWidthAttributeName: -1 // Change here
]
.strokeWidth = 0 yields fill without stroke.
.strokeWidth = positiveNumber yields stroke without fill.
.strokeWidth = negativeNumber yields both stroke and fill.
I'm using swift. Is there any way to change default font/ font size etc of an NSMutableAttributedString? Clearly I could set those values specifically for a given range - but that will then override any specific settings in the string.
You can use like that
let yourAttributes = [NSFontAttributeName: UIFont.preferredFontForTextStyle(UIFontTextStyleHeadline), NSForegroundColorAttributeName: UIColor.blackColor()]
let yourString = NSAttributedString(string: "Here my string !", attributes: yourAttributes)
Thanks