set the font of UItextview - ios

I have multiple textviews in my uiview I need to set the font size of this textviews, so I used the following code but it didn't change the font size,
for (UIView *subview in self.view.subviews) {
if ([subview isKindOfClass:[UITextView class]]) {
((UITextView *)subview).font = [UIFont fontWithName:#"Helvetica" size:12];
}
}

As #jrturton says, perhaps your text views are subviews of a subview, then some kind of recursive code is needed. For example:
-(void) setNewFontToTextViews:(UIView*) theView {
if([[theView subviews] count])
for( UIView * subView in [theView subviews])
[self setNewFontToTextViews:subView] ;
else if([theView isKindOfClass:[UITextView class]])
[(UITextView*)theView setFont:[UIFont fontWithName:#"Helvetica" size:12]];
}

Related

Setting UISearchBar placeholder alignment to NSTextAlignmentLeft

I am trying to set the placeholder text alignment in Objective-C or Swift for iOS 9 and above. I have tried the following:
1.
for (UIView *subview in self.subviews) {
for(id field in subview.subviews) {
if ([field isKindOfClass:[UITextField class]]) {
[(UITextField*)field setTextAlignment:NSTextAlignmentLeft];
}
}
}
2.
[[UITextField appearanceWhenContainedInInstancesOfClasses:#[[UISearchBar class]]] setTextAlignment:NSTextAlignmentLeft];
3.
UITextField *searchTextField = [self valueForKey:#"_searchField"];
UILabel *placeholderLabel = [searchTextField valueForKey:#"_placeholderLabel"];
[placeholderLabel setTextAlignment:NSTextAlignmentLeft];
self.searchBar?.searchTextPositionAdjustment = UIOffset(horizontal: -100, vertical: 0)
But none of these have worked. Is there something else that I am missing? The view debugger shows that the textfield is centered, but changing its frame also has not been working for me.

Changing the colour of UIView that is created dynamically

I am creating a UIView and its subviews (such as UITextfield, UIButton etc.) dynamically( by reading from JSON )
I am giving tags to all UIView and try to call by tag.
For instance I am trying to change the background colour of a view inside another view.
parent view's tag is 1, subview's is 11.
My sample code is as follows;
UIView *temp = [self.view viewWithTag:1];
if([[temp viewWithTag:11] isKindOfClass:[UIView class]])
{
[((UIView*)[temp viewWithTag:11]) setBackgroundColor:[UIColor blackColor]];
}
But there is no change, no effect.
Could you please help me on solving this.
#define PARENT_VIEW_TAG 1
#define CHILD_VIEW_TAG 11
UIView *parentView = [self.view viewWithTag:PARENT_VIEW_TAG];
for (UIView *vw in [parentView subviews]) {
if(vw.tag == CHILD_VIEW_TAG)
{
[vw setBackgroundColor:[UIColor blackColor]];
}
}

iOS - Change text font for each segment in UISegmentController

I got a UISegmentedControl called mySegmentedControl
#property (nonatomic) IBOutlet UISegmentedControl *mySegmentedControl;
with 3 segments, and I would change text font for each segment.. Is possible?
Yes you can ----- Try this
for(uint i=0;i<[mySegmentedControl subviews].count;i++)
{
for(UIView *view in [[[mySegmentedControl subviews] objectAtIndex:i] subviews])
{
if([view isKindOfClass:[UILabel class]])
{
if(i==0) // set First segment font
[(UILabel*)view setFont:[UIFont fontWithName:#"HelveticaNeue-Light" size:27]];
if(i==1) // set Second segment font
[(UILabel*)view setFont:[UIFont fontWithName:#"HelveticaNeue-Light" size:17]];
if(i==2) // set Third segment font
[(UILabel*)view setFont:[UIFont fontWithName:#"HelveticaNeue-Light" size:7]];
}
}
}
You can use any images instead of text. This will give you flexible customization for appearance.
NSArray *segemtImages = [NSArray arrayWithObjects:[UIImage imageNamed:#"1st.png"],[UIImage imageNamed:#"2nd.png"],[UIImage imageNamed:#"3rd.png"], nil];
UISegmentedControl *segmentControl = [[UISegmentedControl alloc] initWithItems:segemtImages];
I don't think that is possible. Your only options are either use a custom open source implementation, or set a custom image with the text for each segment.
I modify #Anand Natan 's code:
In storyboard set the first label "1111111111111" is to change it default width. Because change the label don't change segmentcontroll size.
for(uint i=0;i<[_menuBarTop subviews].count;i++){
for(uint j=0;j<[[[[_menuBarTop subviews] objectAtIndex:i] subviews]count];j++){
UIView* view = [[[_menuBarTop subviews] objectAtIndex:i] subviews][j];
if([view isKindOfClass:[UILabel class]]){
UILabel* label = (UILabel*)view;
if([label.text isEqualToString:#"1111111111111"]){
label.text = LANGLOC(#"videolist_category_choosen");
}else if([label.text isEqualToString:#"2"]){
label.text = LANGLOC(#"videolist_category_followed");
}
break;
}
}
}
NSDictionary *textAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont boldSystemFontOfSize:12], NSFontAttributeName, nil];
[_sgmntStatusUI setTitleTextAttributes:textAttributes forState:UIControlStateNormal];
for ios7 later varsions

Multiple lines of text in UISegmentedControl

is there an easy way, such that each item in my UISegmentedControl for instance,
has multiple lines of text?
Thanks.
ps. I've also checked there's no easy way to change the height of UISegmentedControl?
Say in Code? Changing the style to "Bar" does not suit me, and setFrame does not
seem to work in my case too... :((
pps. This is the approach I tried as recommended by Siba but still have some issues.
for (id segment in [segmentedControl subviews])
{
for (id label in [segment subviews])
{
if ([label isKindOfClass:[UILabel class]])
{
UILabel *label2 = label;
//hear u add any of delegate function to increase the height and other label functionality in this
[label2 setTextAlignment:UITextAlignmentCenter];
[label2 setFont:[UIFont boldSystemFontOfSize:12]];
//to adjust the label size manually with respect to text use below code
CGSize labelSize = CGSizeMake(100, 80);
CGSize theStringSize = [label2.text sizeWithFont:label2.font constrainedToSize:labelSize];
CGRect frame = label2.frame;
frame.size = theStringSize;
label2.lineBreakMode = UILineBreakModeWordWrap;
label2.numberOfLines = 0;
[label2 setText:#"text \n 10%"];
}
}
}
1.You can try the following.
create a multiline UILabel
fill the label with N lines of text
convert the label into an UIImage
set the image as a segments content
Like one Done in This Answer :
UISegmentedControl text with multiple lines?
2.You can Take An ImageView in the back of Segmented Control with Same frame And Change images as per SelectedSegmentedIndex Like below.
But Don't forget to set Alpha of SegmentedControl to 0.05. At ViewDidLoad.
segment_Control.alpha=0.05;
The Coding will be Like this.
- (IBAction)segmented_Changed:(id)sender {
if (segmented_control.selectedSegmentIndex==0) {
segment_image.image=[UIImage imageNamed:#"tab_Act1.png"];
}else
if (segmented_control.selectedSegmentIndex==2) {
segment_image.image=[UIImage imageNamed:#"tab_Act3.png"];
}
else if (segmented_control.selectedSegmentIndex==1) {
segment_image.image=[UIImage imageNamed:#"tab_Act2.png"];
}
}
3.Also You Can try This :
for (id segment in [segmentedControl subviews])
{
for (id label in [segment subviews])
{
if ([label isKindOfClass:[UILabel class]])
{
//hear u add any of delegate function to increase the height and other label functionality in this
[label setTextAlignment:UITextAlignmentCenter];
[label setFont:[UIFont boldSystemFontOfSize:12]];
//to adjust the label size manually with respect to text use below code
CGSize labelSize = CGSizeMake(100, 80);
CGSize theStringSize = [label.text sizeWithFont:label.font constrainedToSize:labelSize];
CGRect frame = label.frame;
frame.size = theStringSize;
}
}
}
Source : Multiline Text On UIsegment Control

iPad - Change all elements' text color on a view simultaneously

Is there a way to have all the text of all the labels, buttons and title's change to a single color with a simple piece of code?
Such as looping through all 'elements' on a view or similar?
I need to be able to toggle the text color of my whole app with a single button. But can't seem to find an efficient way to alter all the elements.
This certainly isn't one or two lines, but it does the trick and is generally what I use.
for(UIView *v in [self.view subviews]) {
if ([v isKindOfClass:[UILabel class]]) {
[(UILabel *)v setTextColor:[UIColor blackColor]];
}
else if ([v isKindOfClass:[UITextView class]]) {
[(UITextView *)v setTextColor:[UIColor blackColor]];
}
//etc...
}

Resources