Integer and string won't right pad.. Objective C - ios

The code is below. What ends up happening is it's as if I used leftPadding instead of rightPadding and a string or long integer will run of the screen to the right. What I want is for the string is pad itself on the right and it's length extends to the left..
- (void)constructTimeStampLabel {
CGFloat rightPadding = 250.f;
CGFloat topPadding = -30.f;
CGRect frame = CGRectMake(rightPadding,
topPadding,
floorf(CGRectGetWidth(__informationView.frame)),
CGRectGetHeight(__informationView.frame) - topPadding);
_timeStampLabel = [[UILabel alloc] initWithFrame:frame];
_timeStampLabel.text = [NSString stringWithFormat:#"Sent on %#", _feedItem.timeStamp];
_timeStampLabel.textColor = [UIColor whiteColor];
[_timeStampLabel setFont:[UIFont systemFontOfSize:12]];
[__informationView addSubview:_timeStampLabel];
}

try setting the alignment to the right.
_timeStampLabel.textAlignment = NSTextAlignmentRight;

Related

Highlight just the text in a UILabel

I'm attempting to set the background color/highlight just the text within a UILabel. The issue is that the line breaks and spaces added to the UILabel to keep the text centered are also being highlighted.
Notice the spacing before the last line in the UILabel is highlighted. Also, the beginning and end of any new lines are also highlighted.
I'm creating the example above with the following code:
-(void)createSomeLabel {
// Create and position my label
UILabel *someLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,
0,
self.view.frame.size.width - 40,
self.view.frame.size.height - 300)];
someLabel.center = CGPointMake(self.view.frame.size.width / 2, self.view.frame.size.height / 2);
someLabel.textAlignment = NSTextAlignmentCenter;
someLabel.textColor = [UIColor whiteColor];
someLabel.lineBreakMode = NSLineBreakByWordWrapping;
someLabel.numberOfLines = 0;
[self.view addSubview:someLabel];
// This string will be different lengths all the time
NSString *someLongString = #"Here is a really long amount of text that is going to wordwrap/line break and I don't want to highlight the spacing. I want to just highlight the words and a single space before/after the word";
// Create attributed string
NSMutableAttributedString *someLongStringAttr=[[NSMutableAttributedString alloc] initWithString:someLongString attributes:nil];
// Apply background color
[someLongStringAttr addAttribute:NSBackgroundColorAttributeName
value:[UIColor colorWithWhite:0 alpha:0.25]
range:NSMakeRange(0, someLongStringAttr.length)];
// Set text of label
someLabel.attributedText = someLongStringAttr;
}
The output I'd like to achieve is to highlight only the text and the spaces between the words if there is only one space. The length of the text and the size of the UILabel will constantly be different so hard coding a solution is not an option unfortunately.
It seemed to me that the line break was the problem.
My idea was to try and know when the UILabel would add a line break and then just remove that character from the range of characters being highlighted.
It appears that you can't just ask UILabel where the line breaks are going to be but you can check what the size of an NSString will be when you add it to a label.
Using this information you can increment through each character constantly checking the height, and when the height changes you know you have a new line.
I have made an example that takes the Label's string and separates it into its individual lines that will appear in the UILabel. Once I have each line, I just set the background color on each line instead of the whole string. This eliminates and background colors being set on line breaks.
There are probably better solutions, and this one could probably be optimized for better performance, but it's a starting point and it appears to work.
- (void)createSomeLabel {
// Create and position my label
UILabel *someLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,
0,
self.view.frame.size.width - 40,
self.view.frame.size.height - 300)];
someLabel.center = CGPointMake(self.view.frame.size.width / 2, self.view.frame.size.height / 2);
someLabel.textAlignment = NSTextAlignmentCenter;
someLabel.textColor = [UIColor whiteColor];
someLabel.lineBreakMode = NSLineBreakByWordWrapping;
someLabel.numberOfLines = 0;
[self.view addSubview:someLabel];
// This string will be different lengths all the time
NSString *someLongString = #"Here is a really long amount of text that is going to wordwrap/line break and I don't want to highlight the spacing. I want to just highlight the words and a single space before/after the word";
// Create attributed string
NSMutableAttributedString *someLongStringAttr=[[NSMutableAttributedString alloc] initWithString:someLongString attributes:nil];
// The idea here is to figure out where the UILabel would automatically make a line break and get each line of text separately.
// Temporarily set the label to be that string so that we can guess where the UILabel naturally puts its line breaks.
[someLabel setText:someLongString];
// Get an array of each individual line as the UILabel would present it.
NSArray *allLines = getLinesForLabel(someLabel);
[someLabel setText:#""];
// Loop through each line of text and apply the background color to just the text within that range.
// This way, no whitespace / line breaks will be highlighted.
__block int startRange = 0;
[allLines enumerateObjectsUsingBlock:^(NSString *line, NSUInteger idx, BOOL *stop) {
// The end range should be the length of the line, minus one for the whitespace.
// If we are on the final line, there are no more line breaks so we use the whole line length.
NSUInteger endRange = (idx+1 == allLines.count) ? line.length : line.length-1;
// Apply background color
[someLongStringAttr addAttribute:NSBackgroundColorAttributeName
value:[UIColor colorWithWhite:0 alpha:0.25]
range:NSMakeRange(startRange, endRange)];
// Update the start range to the next line
startRange += line.length;
}];
// Set text of label
someLabel.attributedText = someLongStringAttr;
}
#pragma mark - Utility Functions
static NSArray *getLinesForLabel(UILabel *label) {
// Get the text from the label
NSString *labelText = label.text;
// Create an array to hold the lines of text
NSMutableArray *allLines = [NSMutableArray array];
while (YES) {
// Get the length of the current line of text
int length = getLengthOfTextInFrame(label, labelText) + 1;
// Add this line of text to the array
[allLines addObject:[labelText substringToIndex:length]];
// Adjust the label text
labelText = [labelText substringFromIndex:length];
// Check for the final line
if(labelText.length<length) {
[allLines addObject:labelText];
break;
}
}
return [NSArray arrayWithArray:allLines];
}
static int getLengthOfTextInFrame(UILabel *label, NSString *text) {
// Create a block for getting the bounds of the current peice of text.
CGRect (^boundingRectForLength)(int) = ^CGRect(int length) {
NSString *cutText = [text substringToIndex:length];
CGRect textRect = [cutText boundingRectWithSize:CGSizeMake(label.frame.size.width, CGFLOAT_MAX)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:#{NSFontAttributeName : label.font}
context:nil];
return textRect;
};
// Get the frame of the string for one character
int length = 1;
int lastSpace = 1;
CGRect textRect = boundingRectForLength(length);
CGFloat oneLineHeight = CGRectGetHeight(textRect);
// Keep adding one character to the string until the height changes, then you know you have a new line
while (textRect.size.height <= oneLineHeight)
{
// If the next character is white space, save the current length.
// It could be the end of the line.
// This will not work for character wrap.
if ([[text substringWithRange:NSMakeRange (length, 1)] isEqualToString:#" "]) {
lastSpace = length;
}
// Increment length and get the new bounds
textRect = boundingRectForLength(++length);
}
return lastSpace;
}
I've faced same issue and found out easier solution without huge performance costs. You can simply add TTTAttributedLabel to your project.
My demo project for the question:
#import "TTTAttributedLabel.h"
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UILabel *label1 = [UILabel new];
label1.textAlignment = NSTextAlignmentCenter;
label1.numberOfLines = 0;
label1.frame = CGRectMake(20, 0, CGRectGetWidth(self.view.frame) - 40, CGRectGetHeight(self.view.frame) / 2.0);
[self.view addSubview:label1];
TTTAttributedLabel *label2 = [TTTAttributedLabel new];
label2.textAlignment = NSTextAlignmentCenter;
label2.numberOfLines = 0;
label2.frame = CGRectMake(20, CGRectGetHeight(self.view.frame) / 2.0, CGRectGetWidth(self.view.frame) - 40, CGRectGetHeight(self.view.frame) / 2.0);
[self.view addSubview:label2];
NSDictionary *attributes = #{NSBackgroundColorAttributeName:[UIColor blackColor], NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:[UIFont systemFontOfSize:32 weight:UIFontWeightBold]};
NSAttributedString *string = [[NSAttributedString alloc] initWithString:#"Some very long string which can contain newlines and some other stuff" attributes:attributes];
label1.attributedText = string;
label2.text = string;
}
#end
Starting at iOS 10.3 the same code in question now produces the desired result. Not sure if this is a bug or a new feature.
-(void)createSomeLabel {
// Create and position my label
UILabel *someLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,
0,
self.view.frame.size.width - 40.0,
self.view.frame.size.height - 300.0)];
someLabel.center = CGPointMake(self.view.frame.size.width / 2.0, self.view.frame.size.height / 2.0);
someLabel.textAlignment = NSTextAlignmentCenter;
someLabel.textColor = [UIColor whiteColor];
someLabel.lineBreakMode = NSLineBreakByWordWrapping;
someLabel.numberOfLines = 0;
[self.view addSubview:someLabel];
// This string will be different lengths all the time
NSString *someLongString = #"Here is a really long amount of text that is going to wordwrap/line break and I don't want to highlight the spacing. I want to just highlight the words and a single space before/after the word";
// Create attributed string
NSMutableAttributedString *someLongStringAttr = [[NSMutableAttributedString alloc] initWithString:someLongString attributes:nil];
// Apply background color
[someLongStringAttr addAttribute:NSBackgroundColorAttributeName
value:[UIColor colorWithWhite:0 alpha:0.25]
range:NSMakeRange(0, someLongStringAttr.length)];
// Set text of label
someLabel.attributedText = someLongStringAttr;
}

kCTUnderlineStyleSingle is not working for some character

I am using NSAttributedstring with the attribute kCTUnderlineStyleSingle.
Character is underlining properly. But some character like g, q, p are not visible properly.
Even user cannot differentiate between g and q.
Is there any solution for that?
You can fix it using this method:
- (void)addUnderlineWithRange:(NSRange)range
{
int lineThickness = [EnvUtils isIpadEnv] ? 2 : 1;
UIColor *lineColor = self.textColor;
NSString *string = self.text;
CGRect labelFrame = self.frame;
CGSize expectedLabelSize = [NSString getMultilineTextSize:[string substringWithRange:range] forMaxSize:labelFrame.size font:self.font];
CGSize expectedLabelFullSize = [NSString getMultilineTextSize:string forMaxSize:labelFrame.size font:self.font];
CGSize xOffset = [NSString getMultilineTextSize:[string substringWithRange:NSMakeRange(0, range.location)] forMaxSize:labelFrame.size font:self.font];
CGFloat originX = (labelFrame.size.width - expectedLabelFullSize.width) / 2 + xOffset.width;
CGFloat originY = expectedLabelSize.height + (labelFrame.size.height - expectedLabelFullSize.height) / 2;
CGRect lineFrame = CGRectMake(originX, originY, expectedLabelSize.width, lineThickness);
UIView *lineView = [[[UIView alloc] initWithFrame:lineFrame] autorelease];
lineView.backgroundColor = lineColor;
[self.superview addSubview:lineView];
}

Create a dynamic row of UITextfields

I would like some help creating a dynamic row of UITextfields, I have been trying to do it myself with some success but mostly failure.
I set a number of rows between 1 - 13 and I would like to create UITextfields that fit inside the width of an iPhone frame width evenly spaced with an even gap to the left and right.
Here is what I have done so far:
- (void) displayViews {
// add scrollview
htmlContainerScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0, 60.0, self.view.frame.size.width, 293.0)];
NSInteger viewcount = [axisString integerValue];
color = 200;
for(int i = 0; i< viewcount; i++) {
color = color + 20;
NSLog(#"%f", color);
CGFloat y = i * 91;
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, y,self.view.frame.size.width, 90.0)];
view.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:(1.0/i)];;
[htmlContainerScrollView addSubview:view];
int cutCount = [cutsString integerValue];
int positions = (self.view.frame.size.width/cutCount);
for (int i = 0; i < cutCount; i++) {
//first one
UITextField *cutField = [[UITextField alloc] initWithFrame:CGRectMake((positions*i)+(20/2), 25, 20, 20)];
cutField.textColor = [UIColor colorWithRed:0/256.0 green:84/256.0 blue:129/256.0 alpha:1.0];
cutField.font = [UIFont fontWithName:#"Helvetica-Bold" size:25];
cutField.backgroundColor=[UIColor whiteColor];
[view addSubview:cutField];
}
}
htmlContainerScrollView.contentSize = CGSizeMake(self.view.frame.size.width, 91 *viewcount);
[self.view addSubview:htmlContainerScrollView];
}
The biggest problem with mine is that it does not evenly space them; they are always to the left.
change your cutField's frame x coordinate value calculation from
(positions*i)+(20/2)
to
((positions*i)-(20/2)+(positions/2)).
that should do the trick.
it's just a matter of the x coordinate calculation and i am sure there is room to optimize that a bit.

Add header to UIPickerView

I created the UIPickerView with 4 components.
Can I add the header title for each component in that UIPickerView?
Thanks for any advice
Swift version, based on #greentor's answer with some improvements:
var labelTexts = ["Months", "Weeks", "Days", "Hours", "Minutes"]
override func viewDidAppear(animated: Bool) {
let labelWidth = itemPicker.frame.width / CGFloat(itemPicker.numberOfComponents)
for index in 0..<labelTexts.count {
let label: UILabel = UILabel.init(frame: CGRectMake(itemPicker.frame.origin.x + labelWidth * CGFloat(index), 0, labelWidth, 20))
label.text = labelTexts[index]
label.textAlignment = .Center
itemPicker.addSubview(label)
}
Note that this might work incorrectly if called in ViewWillAppear or earlier because the UIPickerView won't know its frame dimensions at that point yet.
One other technique I use from time to time is the make the header the first row in each column:
You need to increment the number of rows of course..
I show 3 components and then use a UIView to show a title (UILabel) centred above each component (you can easily amend this for more or fewer components):
-(void)labelForDays
{
NSString *strDay = #"Days";
float lblWidth = self.pickerDayHourMin.frame.size.width / self.pickerDayHourMin.numberOfComponents;
float lblXposition = self.pickerDayHourMin.frame.origin.x;
float lblYposition = (self.pickerDayHourMin.frame.origin.y);
UILabel *lblDay = [[UILabel alloc] initWithFrame:CGRectMake(lblXposition,
lblYposition,
lblWidth,
20)];
[lblDay setText:strDay];
[lblDay setTextAlignment:NSTextAlignmentCenter];
[self.view addSubview:lblDay];
}
Then a label for 2nd component
-(void)labelForHours
{
NSString *strHours = #"Hours";
float lblWidth = self.pickerDayHourMin.frame.size.width / self.pickerDayHourMin.numberOfComponents;
float lblXposition = self.pickerDayHourMin.frame.origin.x;
float lblYposition = (self.pickerDayHourMin.frame.origin.y);
UILabel *lblHours;
if (self.pickerDayHourMin.numberOfComponents ==3)
{
lblHours = [[UILabel alloc] initWithFrame:CGRectMake((lblXposition + lblWidth),
lblYposition,
lblWidth,
20)];
}
[lblHours setText:strHours];
[lblHours setTextAlignment:NSTextAlignmentCenter];
[self.view addSubview:lblHours];
}
and finally a label centred above the 3rd component
-(void)labelForMinutes
{
NSString *strMinute = #"Minutes";
float lblWidth = self.pickerDayHourMin.frame.size.width / self.pickerDayHourMin.numberOfComponents;
float lblXposition = self.pickerDayHourMin.frame.origin.x;
float lblYposition = (self.pickerDayHourMin.frame.origin.y);
UILabel *lblMinute;
if (self.pickerDayHourMin.numberOfComponents ==3)
{
lblMinute = [[UILabel alloc] initWithFrame:CGRectMake((lblXposition + lblWidth + lblWidth),
lblYposition,
lblWidth,
20)];
}
[lblMinute setText:strMinute];
[lblMinute setTextAlignment:NSTextAlignmentCenter];
[self.view addSubview:lblMinute];
}
You can use UIlabel above the UIPicker view,that works well

how create uilabel's dynamically from an NSMutableArray?

NSMutableArray *items // contains 15 items
I need to put one down label from another i try something like this but not work
int count=20;
for(int i = 0; i < [items count]; i++){
UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(0,0,0,count)];
label.text = #"text"; //etc...
count+=20;
[_scroll addSubview:label];
}
What can i do thanks
You need to set the frame properly.
int count=20;
for(int i = 0; i < [items count]; i++){
UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(0,count,0,0)];
label.text = #"text"; //etc...
[label sizeToFit]; // resize the width and height to fit the text
count+=20;
[_scroll addSubview:label];
}
As suggested by rmaddy...adding a new line to adjust the height of the label as well, assumed that you have an NSMutableArray object 'items' containing strings.
float previousLabelHeight = 0.0;
for(int i = 0; i < [items count]; i++){
CGSize theSize = [[items objectAtIndex: i] sizeWithFont:[UIFont systemFontOfSize:17.0] constrainedToSize:CGSizeMake(320, FLT_MAX) lineBreakMode:UILineBreakModeWordWrap]; //can adjust width from 320 to whatever you want and system font as well
float newLabelHeight = previousLabelHeight + theSize.height;
UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(0,newLabelHeight,0,0)];
label.text = [items objectAtIndex: i];
[label sizeToFit]; // resize the width and height to fit the text
previousLabelHeight = newLabelHeight + 5 //adding 5 for padding
[_scroll addSubview:label];
}
Cheers,
Happy Coding.
I think you're trying to change the frame's Y value, but the last parameter of CGRectMake() is the rect's height. You want the second parameter.
Here is the Swift version to add labels dynamically from array..
var previousLabelHeight: CGFloat = 0.0;
for dict in items {
let text: String = "Some text to display in the UILabel"
let size = heightNeededForText(text as NSString, withFont: UIFont.systemFontOfSize(15.0), width: scrollView.frame.size.width - 20, lineBreakMode: NSLineBreakMode.ByWordWrapping)
let newLabelHeight = previousLabelHeight + size;
let label = UILabel(frame: CGRectMake(0, newLabelHeight, 0, 0))
label.text = text
label.sizeToFit() // resize the width and height to fit the text
previousLabelHeight = newLabelHeight + 5 //adding 5 for padding
scroll.addSubview(label)
}
As sizeWithFont: ConstraintedToSize is deprecated from ios 7.0,
we have to use boundingRectWithSize method from NSString....
func heightNeededForText(text: NSString, withFont font: UIFont, width: CGFloat, lineBreakMode:NSLineBreakMode) -> CGFloat {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineBreakMode = lineBreakMode
let size: CGSize = text.boundingRectWithSize(CGSizeMake(width, CGFloat.max), options: [.UsesLineFragmentOrigin, .UsesFontLeading], attributes: [ NSFontAttributeName: font, NSParagraphStyleAttributeName: paragraphStyle], context: nil).size//text boundingRectWithSize:CGSizeMake(width, CGFLOAT_MA
return ceil(size.height);
}

Resources