[![enter image description here][1]][1]Can any one share their code relating to show round Badge number icon on UIButton on Top right Corner Using CALayer And CATextLayer and Add to UIButton
Thanks In advance
For your badge, you can use only label for showing the badge on button specially.
let label = UILabel()
label.clipsToBounds = true
label.layer.cornerRadius = label.font.pointSize * 1.2 / 2
label.backgroundColor = UIColor.grayColor()
label.textColor = UIColor.whiteColor()
label.text = " Some Text "; // note spaces before and after text
And For reusability, you can create the category or extension for this code.
Related
I am trying to make a form and the requirement is to have a UItextfield with multiple lines which means, when a specific number of characters are entered in one line the blip moves to next line also on hitting enter it should enter the next line as well.
Currently, I am working with Xcode 9.4 but it offers a single line text field.
I used UITextView with this code:
lazy var commentTextView: UITextView = {
// Create a TextView.
let textView: UITextView = UITextView()
// Round the corners.
textView.layer.masksToBounds = true
// Set the size of the roundness.
textView.layer.cornerRadius = 20.0
// Set the thickness of the border.
textView.layer.borderWidth = 1
// Set the border color to black.
textView.layer.borderColor = UIColor.systemGray.cgColor
// Set the font.
textView.font = UIFont.systemFont(ofSize: 16.0)
// Set font color.
textView.textColor = UIColor.black
// Set left justified.
textView.textAlignment = NSTextAlignment.left
// Automatically detect links, dates, etc. and convert them to links.
textView.dataDetectorTypes = UIDataDetectorTypes.all
// Set shadow darkness.
textView.layer.shadowOpacity = 0.5
// Make text uneditable.
textView.isEditable = true
return textView
}()
and this is the result
You should use UITextView for multiline string or there is third party library which you can use.
MultilineTextField
This post is a separate topic but related to Custom Nav Title offset ios 11
I created a new thread because it is a separate issue.
From project: https://github.com/ekscrypto/Swift-Tutorial-Custom-Title-View
To recreate the problem, simply put a button on the existing Root View Controller that pushes another view controller. The "< Back" button scoots the title over, which makes it terribly uncentered. How can I fix this? Thank you.
Simple change required to support earlier versions of iOS; you should properly resize your custom title view to be the expected width its actually going to be. iOS 11 attempts to resize the width of the title view to fit the available space based on the constraints but iOS 10 and below will try to maintain the size of the view as much as possible.
The solution is therefore to open the MyCustomTitleView.xib file, and to set the width of the MyCustomTitleView to something reasonable like 180pt.
Cheers!
For iOS 10 and below you need to set up CGFrame for your attributed titleLabel.
Here is the code example.
- (void)viewDidLoad {
[super viewDidLoad];
UILabel *titleLabel = [[UILabel alloc]init];
NSDictionary *fontAttribute = #{ NSFontAttributeName:[UIFont fontWithName:#"SFProText-Medium" size:15.f]};
NSAttributedString *str = [[NSAttributedString alloc]initWithString:#"YOUR TITLE"
attributes:fontAttribute];
titleLabel.attributedText = str;
[titleLabel sizeToFit]; // This method create a frame
self.navigationItem.titleView = titleLabel;
}
Swift example:
override func viewDidLoad() {
super.viewDidLoad()
let titleLabel = UILabel()
let title = NSMutableAttributedString(string: "Your title", attributes:[
NSAttributedStringKey.foregroundColor: UIColor.blue,
NSAttributedStringKey.font: UIFont.systemFont(ofSize: 17.0, weight: UIFont.Weight.light)])
titleLabel.attributedText = title
titleLabel.sizeToFit()
self.navigationItem.titleView = titleLabel
}
How to add background color and rounded corners for Strings in iOS?
like this picture below:
Can't do it with NSAttributedstring. So, should I use CoreText? I found this answer but still don't know how to do it.
String is just a data in text format, it's not visual. You need labels, those are labels there. And then set your string to that label.
let label = UILabel()
label.backgroundColor = .green
label.text = "Label" // You set your string to your label
label.layer.cornerRadius = 5
label.layer.borderColor = .clear
label.layer.borderWidth = 1
label.layer.clipsToBounds = true
Code above creates a label and sets everything you need to achieve it like in the picture. Now you need to add this label into your view or you can set these properties to an existing label you got.
EDIT:
If you want to make individual text inside a text view colorful, you can use TTTAttributedLabel and set an attributed text with background to individual pieces of text.
You can find TTTAttributedLabel here.
CornerRadius: kTTTBackgroundCornerRadiusAttributeName
BackgroundColor: kTTTBackgroundFillColorAttributeName
Example usage:
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithAttributedString:TTTAttributedTestString()];
[string addAttribute:kTTTBackgroundFillColorAttributeName value:(id)[UIColor greenColor].CGColor range:NSMakeRange(0, [string length])];
How can I make a background under a UIBarButtonItem like this top left button:
You should tell your designer to create that type of button image.
UINavigationBar.appearance().layer.shadowColor = UIColor.blackColor().CGColor
UINavigationBar.appearance().layer.shadowOffset = CGSizeMake(1, 1)
UINavigationBar.appearance().layer.shadowRadius = 3
UINavigationBar.appearance().layer.shadowOpacity = 1
How to use RubyMotion to set a textField border color?
Example code:
textField = UITextField.alloc.init
textField.borderStyle = UITextBorderStyleLine
The border color shows up as the Apple default color; I want it to be red.
This is using RubyMotion, not Objective-C
Use the Quartz framework.
In your Rakefile:
app.frameworks << 'QuartzCore'
To set the layer attributes:
textField.layer.borderColor = UIColor.redColor.CGColor
textField.layer.borderWidth = 1
textField.layer.masksToBounds = true