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.
Related
I have an issue with stroke color on iOS 14.
let attributes: [NSAttributedString.Key: Any] = [
.foregroundColor: UIColor.white,
.strokeColor: UIColor.black,
.font: UIFont.systemFont(ofSize: 65, weight: .black),
.strokeWidth: -1
]
lableNumber.attributedText = NSAttributedString(string: "2", attributes: attributes)
iOS 13
iOS 14
It is working on iOS 13 but not working in iOS 14. Can anyone help me to resolve this issue?
Looks like Apple has changed the font outlines in iOS 14, the glyphs for 6 and 9 exhibit similar artifacts. I would file a bug with Apple.
Depending on your requirements, using UIFont.monospacedSystemFont(...) could work, those glyphs still look OK to me. (Edit: "1" has artifacts in the monospaced version as well. Maybe use UIFont(name: "HelveticaNeue-Bold", size: 65) instead?)
I am trying to use attributed text with UITextField. I have got my placeholder text customised but it's ignoring the values I set for the main text property.
nameTextField.attributedText = NSAttributedString(string: "", attributes: [NSForegroundColorAttributeName : UIColor.white, NSParagraphStyleAttributeName : paraStyle, NSFontAttributeName : UIFont.init(name: "HelveticaNeue-Bold", size: 16)]);
Strangely there is also an issue in the storyboard. If I set the values on the text field it loses them as soon as I click away.
Anyone experienced this before?
You want to set the typingAttributes, not the attributedString.
Try this:
func textFieldDidBeginEditing(_ textField: UITextField) {
let paraStyle: NSParagraphStyle = NSParagraphStyle()
textField.typingAttributes = [NSForegroundColorAttributeName : UIColor.white, NSParagraphStyleAttributeName : paraStyle, NSFontAttributeName : UIFont.init(name: "HelveticaNeue-Bold", size: 16)]
}
Solution:
textField.defaultTextAttributes = yourAttributes // call in viewDidLoad
#DonMag's solution works but causes memory usage issues and app starts to lag (a lot). So using typingAttributes, at some point in time, the memory usage went up and never stopped and caused the app to freeze.
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
]
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.
Here I have tried to append two attributed strings in a single UILabel but I made some mistake so any one can find my mistake please?
let textFontAttributes = [
NSFontAttributeName : font,
// Note: SKColor.whiteColor().CGColor breaks this
NSForegroundColorAttributeName: UIColor.whiteColor(),
NSStrokeColorAttributeName: UIColor.blackColor(),
// Note: Use negative value here if you want foreground color to show
NSStrokeWidthAttributeName: -8]
This is the demo, you can have a try, In fact, you should provide more info of what the wrong is or what the error is.
var firstString = "Hello" as NSString
var secondString = " World" as NSString
var totalString = firstString.stringByAppendingString(secondString as String) as NSString
let firsttAttributes = [
NSFontAttributeName : UIFont.systemFontOfSize(16),
// Note: SKColor.whiteColor().CGColor breaks this
NSForegroundColorAttributeName: UIColor.whiteColor(),
NSStrokeColorAttributeName: UIColor.blackColor(),
// Note: Use negative value here if you want foreground color to show
NSStrokeWidthAttributeName: -8]
let secondAttributes = [
NSFontAttributeName : UIFont.systemFontOfSize(12),
// Note: SKColor.whiteColor().CGColor breaks this
NSForegroundColorAttributeName: UIColor.redColor(),
NSStrokeColorAttributeName: UIColor.blackColor(),
// Note: Use negative value here if you want foreground color to show
NSStrokeWidthAttributeName: -8]
var attributedString = NSMutableAttributedString(string: totalString as String, attributes: firsttAttributes)
var secondRange = totalString.rangeOfString(secondString as String)
if secondRange.location != NSNotFound {
attributedString.addAttributes(secondAttributes, range: secondRange)
}