Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have 2 UIButton as shown in below image. Here I need to customize exactly the same for both, mean texts, how could I do this.
Suggest how could I do this either in storyboard (using label/view ..) or programmatically. Guide me with some piece of code for NSAttributtedstring in case of label or button text as shown in image.
You can use this below func :
extension UILabel {
func setAttributes(price : String) {
let font:UIFont? = UIFont.boldSystemFont(ofSize: 22)
let fontSuper:UIFont? = UIFont.boldSystemFont(ofSize: 15)
let aDotRange = (price as NSString).range(of: ".")
let attString:NSMutableAttributedString = NSMutableAttributedString(string: price, attributes: [.font:font!])
attString.setAttributes([.font:fontSuper!,.baselineOffset:5], range: NSRange(location:0,length:1))
attString.setAttributes([.font:fontSuper!,.baselineOffset:5],
range: NSRange(location:aDotRange.location,length:4))
attString.setAttributes([.font:font!],
range: NSRange(location:1,length:aDotRange.location - 1 ))
attString.setAttributes([.font:fontSuper!],
range: NSRange(location:aDotRange.location + 4,
length: (price.count) - (aDotRange.location + 4) ))
self.attributedText = attString
}
}
Dummy code :
lblPrice.setAttributes(price: "$249.99 / mon")
lblPrice.layer.cornerRadius = 4
lblPrice.layer.borderWidth = 1.0
lblPrice.layer.borderColor = UIColor.blue.cgColor
lblPrice2.setAttributes(price: "$999.99 / 6 mo")
lblPrice2.layer.cornerRadius = 4
Output :
You can use UIViews and UILabels for designing the buttons and add tapGestures to the UIViews for User interactions.
Related
This question already has an answer here:
How to use NSUnderlineStyle.PatternDot
(1 answer)
Closed 2 years ago.
I Want to doted underline on label in Swift
//you can add doted underline string like this in swift 5
let string = NSMutableAttributedString(string: "Hello string")
string.addAttributes([.underlineStyle : NSUnderlineStyle.single.union(.patternDot).rawValue],
range: NSRange(location: 0, length: string.length))
yourLableName.attributedText = string
You can use NSAttributedString for setting underline in swift 5.
let string = NSAttributedString(string: "Receiver", attributes: [.underlineStyle: NSUnderlineStyle.single.union(.patternDash).rawValue])
label.attributedText = string
This question already has answers here:
How to find actual number of lines of UILabel?
(16 answers)
Closed 5 years ago.
I have a UILabel with dynamic width and dynamic string which I am getting from server. Now I want to set this string to label as text. Also I need to set number of lines to label. As I am getting dynamic string so I am not sure how much characters can be set in a line.
I found similar answers but those are not giving me accuracy as I need.
Set number of lines to 0, this will make it dynamic. You also need to make your UILabel sizeToFitmyUiLabel.sizeToFit()
If you for some reason need the number of lines, you can use following extension on UILabel:
extension UILabel {
func linesNeeded() -> Int {
self.layoutIfNeeded()
let myText = self.text! as NSString
let boundingRectangle = CGSize(width: self.bounds.width, height: CGFloat.greatestFiniteMagnitude)
let labelSize = myText.boundingRect(with: boundingRectangle,
options: .usesLineFragmentOrigin,
attributes: [NSAttributedStringKey.font: self.font],
context: nil)
return (labelSize.height / self.font.lineHeight)
}
}
However, if you don't need the height, you just want to allow the label to show the whole text, you should set label.numberOfLines = 0 which will allow the label to grow as needed.
This question already has answers here:
NSMutableAttributedString not working in tableviewcell with a specific range
(1 answer)
iOS 10.3: NSStrikethroughStyleAttributeName is not rendered if applied to a sub range of NSMutableAttributedString
(7 answers)
Closed 5 years ago.
Well, it is very strange for me that, NSStrikethroughStyleAttributeName doesn't working if I concatenate/ append two NSMutableAttributedString for a label.
let partTwo: NSMutableAttributedString = NSMutableAttributedString(string: "4450")
partTwo.addAttribute(NSStrikethroughStyleAttributeName, value: 1, range: NSMakeRange(0, partTwo.length))
cell.lbl11.attributedText = partTwo
Above code working perfect, but if I do bellow code:
let partOne: NSMutableAttributedString = NSMutableAttributedString(string: "WAS $")
let partTwo: NSMutableAttributedString = NSMutableAttributedString(string: "4450")
partTwo.addAttribute(NSStrikethroughStyleAttributeName, value: 1, range: NSMakeRange(0, partTwo.length))
let concate = NSMutableAttributedString(attributedString: partOne)
concate.append(partTwo)
cell.lbl11.attributedText = concate
NSStrikethroughStyleAttributeName doesn't working.
I needs like below image in one single label. Is it possible?
This question already has answers here:
Example of NSAttributedString with two different font sizes?
(4 answers)
ios swift: Is it possible to change the font style of a certain word in a string?
(3 answers)
Closed 7 years ago.
Example:
let dummy = UILabel()
dummy.text = "this is a String"
I want the view to show something like this. Also, is it possible to change the font size of "String" in dummy?
this is a String
You can use NSAttributedString to do that:
let dummy = UILabel()
let theString = "this is a String"
let attributedString = NSMutableAttributedString(string: theString, attributes: [NSFontAttributeName:UIFont.systemFontOfSize(12.0)])
let boldFontAttribute = [NSFontAttributeName: UIFont.boldSystemFontOfSize(24.0)]
attributedString.addAttributes(boldFontAttribute, range: NSMakeRange(10, theString.characters.count))
dummy.attributedText = attributedString
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Can anyone suggest me how to create a 4 digit pin entry screen like One time password, User will enter and one circle will be checked/darken.
I know I can create this by using images and hidden text field. But I am unable to decide best way for the same.
Any help will be appreciated.
Imagine you have a UITextField. Getting the user's input to show up as black dots (and not expose their information) is as easy as doing this (assuming the UITextField is called field, and that you're using Swift).
field.secureTextEntry = true
Honestly I'm not totally sure what you're asking but this is my best guess.
My best guess to achieve this using image. You can simply add image on your UITextField's subview.
func addImageToTextField(txtPin : UITextField)
{
let imageView = UIImageView();
let image = UIImage(named: "darkImage.png");
imageView.image = image;
imageView.frame = CGRect(x: 0, y: 0, width: txtPin.frame.width, height: txtPin.frame.height)
txtPin.addSubview(imageView)
}
And call this method from textFieldshouldChangeCharactersInRange
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
textField.textColor = UIColor.clearColor()
if (!string.isEmpty) {
self.addImageToTextField(textField)
}
Similarly you can do the same if user hits back button to clear the textfield
I have done this and works like a charm for me. Cheers !! happy coding.