I want to add a custom label (with a time stamp) in each cell as well as an image (for warning message) in JSQMessageViewController. I am already using bottomlabel as well as toplabel. But I am unable to get the result I want.
The image is a reference of what I would like it to look like
UIImage *bubbleImage = [UIImage imageNamed:#"Commentbox_right.png"];
UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(textFrame.origin.x-2, textFrame.origin.y-2, textFrame.size.width+4 , textFrame.size.height+7)];
imgView.image= [bubbleImage stretchableImageWithLeftCapWidth:bubbleImage.size.width/2-5 topCapHeight:bubbleImage.size.height/2];
[cell addSubview:imgView];
[cell bringSubviewToFront:txtViewMessage];
UILabel *lblTimeStamp = [[UILabel alloc]initWithFrame:CGRectMake(textFrame.origin.x+2, imgView.frame.size.height+imgView.frame.origin.y, 90, 10)];
[lblTimeStamp setText:message.dateTime];//set time here
[lblTimeStamp setFont:FONT_300_LIGHT(7)];
[lblTimeStamp setTextColor:GET_COLOR_WITH_RGB(129,129,129, 1)];
[lblTimeStamp setTextAlignment:NSTextAlignmentLeft];
[lblTimeStamp setBackgroundColor:[UIColor clearColor]];
[cell addSubview:lblTimeStamp];
Sorry i didn't get much time to look in to it .. but again you can add that image in same message bubble xib and add constraints according to your need. Try this xib
How I add custom Label in JSQMessageviewcontroller is... I declare Label text before ViewDidLoad.
// Add Text Label
let myLabel: UILabel = {
let lb = UILabel()
lb.translatesAutoresizingMaskIntoConstraints = false
lb.textAlignment = .center
lb.numberOfLines = 1
lb.textColor = UIColor.white
lb.font=UIFont.systemFont(ofSize: 22)
lb.backgroundColor = UIColor(red: 0.0/255.0, green:70.0/255.0, blue:110.0/255.0, alpha:1)
lb.text = NSLocalizedString("No Notification", comment: "")
return lb
}()
And add this code in viewDidiLoad or call anywhere you like.
self.view.addSubview(myLabel)
setUpMyLabel()
This is how I add custom label in my app. Hope this will help you. :)
Related
Hi i am beginner in ios and i am trying to insert UItextfield "text" data exactly at center of the y-axis as like below textfield image
And i would like to insert UIbutton "titleLabel text" exactly center of x-axis and center of y-axis as like below button image for this i tried lot but i did not get result please help me some one
my code:-
[self.TripIdTxt setValue:[UIColor blackColor] forKeyPath:#"_placeholderLabel.textColor"];
self.TripIdTxt.layer.sublayerTransform = CATransform3DMakeTranslation(10.0f, 0.0f, 0.0f);
self.TripIdTxt.backgroundColor = [UIColor whiteColor];
self.TripIdTxt.layer.cornerRadius = 3.0f;
self.TripIdTxt.layer.masksToBounds = YES;
[self.TripIdTxt setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
[self.TripIdTxt setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
self.TripIdTxt.textColor = [UIColor blackColor];
self.TripIdTxt.font = [UIFont fontWithName:bitter_Regular size:14];
set
for UIButton
yourbtnName.titleLabel.textAlignment = NSTextAlignmentCenter;
for y-axis
[yourbtnName setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
for X-axis
[yourbtnName setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
for UITextFields
yourtextfieldName.textAlignment = NSTextAlignmentCenter;
diectly you can access in storyboard like
for additional information see this link
Padding with TextField
UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 20)];
TripIdTxt.leftView = paddingView;
self. TripIdTxt.leftViewMode = UITextFieldViewModeAlways;
or else use Transform
TripIdTxt.layer.sublayerTransform = CATransform3DMakeTranslation(10.0f, 0.0f, 0.0f);
Set the text alignmaent center of your lable property .
mainlabel.textAlignment = NSTextAlignmentCenter;
u set its alignment using xib by
u can also set its vertical alignment using xib by
Programmatically U can set Content's vertical alignment for textfield as well as button as
button.contentVerticalAlignment = UIControlContentVerticalAlignmentBottom;
txtField.contentVerticalAlignment = UIControlContentHorizontalAlignmentRight;
Hope it helps.
Happy Coding.. :)
I have a UITableView where I would like to set the tableView title to a wide label.
I'm trying to do something like:
"Daily Schedule - Long Station Name"
Ideally, this would mean a title and a subTitle. As there isn't a subtitle field, is there anyway to display a wide label?
Please note that I am not asking about UITableView Section Headers.
This is about the TableViewControllertitle.
Thanks
I think you are referring to the title displayed by the UINavigationController. In that case, you can get away with a custom title view, in which you can have two lines in the text label. Here is an example I'm currently using.
// Title label is the view we will assign to the titleView property
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 140, 50)];
titleLabel.numberOfLines = 2;
titleLabel.text = #"Daily Schedule - Long Station Name";
titleLabel.textColor = [UIColor whiteColor];
titleLabel.font = [UIFont fontWithName:#"Montserrat-Regular" size:22];
titleLabel.adjustsFontSizeToFitWidth = YES;
titleLabel.minimumScaleFactor = 0.8;
titleLabel.textAlignment = NSTextAlignmentCenter;
self.navigationItem.titleView = titleLabel;
Edit or delete the attributes to get the effects you want with your title.
I would like create a button with two independent labels.
I want to set in one button, two separate texts in different colors.
e.g.
[myButton setTitle: #"Title" forState: UIControlStateNormal];
[myButton setTitle2: #"Title2" forState: UIControlStateNormal];
Is it even possible? Maybe I should need to create a new control? Can anyone help me? I will be very grateful for a tutorial step by step.
If you wanted to do it all in Interface Builder you could place two UILabels where you want them in the view and then lay a UIButton over the top of them. Then with the Attributes Inspector change the UIButton's type to "Custom" and delete any existing label or placeholder text it has. This will make the button clear so both labels show through and you can set the UIButton's IBAction to do whatever you want to the two separate UILabels.
Also, you can do this :
UIButton *testButton = [UIButton buttonWithType:UIButtonTypeCustom];
testButton.frame = CGRectMake(0, 0, 200, 40);
[self.view addSubview:testButton];
UILabel *firstLineTestButton = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 20)];
firstLineTestButton.text = #"First line";
firstLineTestButton.font = [UIFont systemFontOfSize:12];
[testButton addSubview:firstLineTestButton];
UILabel *secondLineTestButton = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, 200, 20)];
secondLineTestButton.text = #"Second line";
secondLineTestButton.font = [UIFont boldSystemFontOfSize:12];
[testButton addSubview:secondLineTestButton];
but the #vikingosegundo's solution is more appropriate for the new SDK
For iOS 6 and higher you can use setAttributedTitle:forState: to set an attributes string. A NSAttributedString can have many different attributes like text color.
Swift Version:
The following code also centres two titles both horizontally and vertically inside the button and has some font style adjustments.
let lineOne = UILabel.init(frame: CGRect(x: 0, y: self.button.frame.size.height / 4, width: self.button.frame.size.width, height: self.button.frame.size.height / 4))
lineOne.text = "Title 1"
lineOne.textColor = .black
lineOne.font = UIFont.boldSystemFont(ofSize: 13.0)
lineOne.textAlignment = .center
let lineTwo = UILabel.init(frame: CGRect(x: 0, y: lineOne.frame.size.height * 2, width: self.button.frame.size.width, height: self.button.frame.size.height / 4))
lineTwo.text = "Title 2"
lineTwo.textColor = .black
lineTwo.font = UIFont.boldSystemFont(ofSize: 13)
lineTwo.textAlignment = .center
self.button.addSubview(lineOne)
self.button.insertSubview(lineTwo, belowSubview: lineOne)
UIButton is a subclass of UIView. So you can add two different labels to your button using - (void)addSubview:(UIView *)view. Positioning could then be done either with Autolayout (if you are using that), or frames + springs/struts.
I might missing something very trivial here, but I can't figure out how to get the width of my UILabel. Note that is it being added programatically, and the it's size fits the text inside it (the text vary from 2 to 35 characters). It automatically fits the right width of it's content, but I need to get the width.
My code to add it to the screen:
UILabel *textLabel = [[UILabel alloc] init];
textLabel.text = textInputFromUser;
textLabel.textColor = [UIColor whiteColor];
textLabel.backgroundColor = [UIColor colorWithRed:40.0/255.0 green:40.0/255.0 blue:40.0/255.0 alpha:0.95];
textLabel.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:textLabel];
Note that the following code (with "Bounds"/"Frame") always returns 0.00):
NSLog(#"textlabel width: %f", textLabel.bounds.size.width);
You can only find out the size after the first layout pass. So either wait for that or call
[textLabel layoutIfNeeded];
to layout it immediately. After that the frame should be set to the right value.
Since no one else answered, I'll throw in my two cents…
I assume the reason you're unable to get the frame of the label is because the frame hasn't technically been instantiated; but nonetheless, maybe just getting the size of the content would be sufficient for your needs.
Here's a post explaining how to use boundingRectWithSize:options:attributes:context: to get the size of the text in your label: https://stackoverflow.com/a/18961502/2274694
Try initializing the label with a dummy frame like so:
UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,1,1)];
textLabel.text = textInputFromUser;
[textLabel sizeToFit];
or you could try:
UILabel *textLabel = [[UILabel alloc] initWithString:textInputFromUser];
for swift I had to add following code block to get correct width for UILabel:
private var infoLabel = UILabel()
infoLabel.translatesAutoresizingMaskIntoConstraints = false
infoLabel.frame = .zero
infoLabel.font = UIFont.boldSystemFont(ofSize: 14)
infoLabel.numberOfLines = -1
infoLabel.text = "infoText"
infoLabel.sizeToFit()
to get correct width:
let infoWidth = infoLabel.frame.size.width
I also added correct constraints to my label (which is infoLabel) according to design given to me.
I am trying to disable editing on my UITextView. I have tried [aboutStable setUserInteractionEnabled: NO], but it causes the page to not be accessible.
Here is the current code.
- (void)loadTextView1 {
UITextView *textView1 = [[UITextView alloc] init];
[textView1 setFont:[UIFont fontWithName:#"Helvetica" size:14]];
[textView1 setText:#"Example of editable UITextView"];
[textView1 setTextColor:[UIColor blackColor]];
[textView1 setBackgroundColor:[UIColor clearColor]];
[textView1 setTextAlignment:UITextAlignmentLeft];
[textView1 setFrame:CGRectMake(15, 29, 290, 288)];
[self addSubview:textView1];
[textView1 release];
}
First of all, you are using setter methods when you could just be using properties. Secondly, you are setting a whole bunch of unnecessary properties that are very close to the default. Here is a much simpler and perhaps what you intended with your code:
Objective-C
- (void)loadTextView1 {
UITextView *textView1 = [[UITextView alloc] initWithFrame:CGRectMake(15, 29, 290, 288)];
textView1.text = #"Example of non-editable UITextView";
textView1.backgroundColor = [UIColor clearColor];
textView1.editable = NO;
[self addSubView:textView1];
[textView1 release];
}
Swift
func loadTextView1() {
let textView1 = UITextView(frame: CGRect(x: 15, y: 29, width: 290, height: 288))
textView1.text = "Example of non-editable UITextView"
textView1.backgroundColor = .clear
textView1.isEditable = false
addSubView(textView1)
}
You can use the property editable
textView.editable = NO;
Swift 2.0 Version
self.textView.editable = false
More details can be found in the apple UIKit Framework Reference.
Additional UITextView Attributes to consider:
text
attributedText
font
textColor
editable
allowsEditingTextAttributes
dataDetectorTypes
textAlignment
typingAttributes
linkTextAttributes
textContainerInset
For Swift 3.0 and Swift 4.0:
textView.isEditable = false
If you are using interface builder, you can just uncheck "Editable" in the Attributes Inspector.
Wow, this one's certainly being done to death! Sorry to add another but I should mention you can remove any user uncertainty with:
commentTextView.isUserInteractionEnabled = false
Swift 3.0
func loadTextView1()
{
let textView1 = UITextView()
textView1.text = "Example of non-editable UITextView"
textView1.backgroundColor = UIColor.clear
textView1.frame = CGRect(x: 15, y: 29, width: 290, height: 288)
textView1.isEditable = false
addSubView(textView1)
}
Otherwise in Xcode's Interface Builder uncheck "Editable" in the Attributes Inspector for the text view.
If you want to prevent all user interaction you need to do the 2 following things:
self.commentText.isEditable = false
self.commentText.isSelectable = false
cell.txtViewAllGyms.isEditable = false