I was wondering if it was possible to change the fonts on about 100 different viewcontrollers all at once? It would be a lot eisier than going through one by one and changing them. Any ideas? Thank you!
The user interface files (*.xib) are plain text and you can load them into an editor.
In Xcode4 on the left pane you can right-click > open as > source.
This will give you the XML source any you can find/replace there.
Warning: doing something wrong may render the whole file useless, so unless you have source control anyway, make copies of the XIB before attempting changes.
you cant change all the fonts at once....
But i have find one more varient that will help you...
I have made some recursive functions thy can help you..
follow following steps..
First create a class(BaseViewController) extended from UIViewController like in BaseViewController.h file
#interface BaseViewController : UIViewController
And in BaseViewController.m file write following code.
- (void)viewDidLoad
{
[super viewDidLoad];
[self changeFontsOfViewController];
}
-(void)changeFontsOfViewController
{
UIViewController * vv = [self viewControllerOfView:self.view];
NSArray *objects = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([vv class]) owner:vv options:nil];
for (id object in objects)
{
[self changeFontOfView:object];
}
}
-(void)changeFontOfView:(UIView *)aView
{
for (UIView *vv in [aView subviews])
{
if ([vv isKindOfClass:[UIButton class]])
{
UIButton *btn = (UIButton *)vv;
CGFloat fontSize = btn.titleLabel.font.pointSize;
btn.titleLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:fontSize];
}
else if ([vv isKindOfClass:[UILabel class]])
{
UILabel *lbl = (UILabel *)vv;
CGFloat fontSize = lbl.font.pointSize;
[lbl setFont:[UIFont fontWithName:#"Helvetica-Bold" size:fontSize]];
}
else if ([vv isKindOfClass:[UITextView class]])
{
UITextView *txt = (UITextView *)vv;
CGFloat fontSize = txt.font.pointSize;
[txt setFont:[UIFont fontWithName:#"Helvetica-Bold" size:fontSize]];
}
else if ([vv isKindOfClass:[UITextField class]])
{
UITextField *txt = (UITextField *)vv;
CGFloat fontSize = txt.font.pointSize;
[txt setFont:[UIFont fontWithName:#"Helvetica-Bold" size:fontSize]];
}
else if ([vv isKindOfClass:[UIView class]]||[vv isKindOfClass:[UIScrollView class]])
{
if (aView.subviews.count == 0)return;
[self changeFontOfView:vv];
}
}
}
Now your every viewController(RootViewController) will be extended from BaseViewController class like in RootViewController.h..
#import "BaseViewController.h"
#interface RootViewController : BaseViewController
{
}
And make sure that you have written following in your .m file of your UIViewController(RootViewController.m)
- (void)viewDidLoad
{
[super viewDidLoad];
}
Please follow above steps carefully you will rock.......
How about this little recursive method on UIView?
#implementation UIView (JPCSetFont)
- (void)jpc_setAllFonts:(UIFont *)font
{
if ([self respondsToSelector:#selector(setFont:)]) {
UIFont *oldFont = [self valueForKey:#"font"];
UIFont *newFont = [font fontWithSize:oldFont.pointSize];
[self setValue:newFont forKey:#"font"];
}
for (UIView *subview in self.subviews) {
[subview jpc_setAllFonts:font];
}
}
#end
Code by John Cromartie is a good stuff. Simple and laconic.
Just keep in mind some fonts are bold or italic. So you probably have to pass 2 (or 3) params: for regular font (as it is now), for bold font and for the italic one. Besides you have to detect if the current font is bold, etc. So there is a bit enhanced piece of code below.
However, it can work wrong on iOS7 because user can set his own fonts and, as a result, weight / style detection won't work properly.
#implementation UIView (JPCSetFont)
- (void)jpc_setAllFonts:(UIFont*)regular bold:(UIFont*)bold
{
if ([self respondsToSelector:#selector(setFont:)]) {
UIFont *oldFont = [self valueForKey:#"font"];
UIFont *newFont;
// for iOS6
NSRange isBold = [[oldFont fontName] rangeOfString:#"Bold" options:NSCaseInsensitiveSearch];
// for iOS7 (is device owner didn't change it!)
NSRange isMedium = [[oldFont fontName] rangeOfString:#"MediumP4" options:NSCaseInsensitiveSearch];
if (isBold.location==NSNotFound && isMedium.location==NSNotFound) {
newFont = [regular fontWithSize:oldFont.pointSize];
} else {
newFont = [bold fontWithSize:oldFont.pointSize];
}
// TODO: there are italic fonts also though
[self setValue:newFont forKey:#"font"];
}
for (UIView *subview in self.subviews) {
[subview jpc_setAllFonts:regular bold:bold];
}
}
#end
https://github.com/iutinvg/ZZLib/blob/master/ZZLib/UIView%2BZZFontSetter.h
https://github.com/iutinvg/ZZLib/blob/master/ZZLib/UIView%2BZZFontSetter.m
Related
I have a UITextField for the UISearchBar which this was working until iOS 7 upgrade and now it fails at this line: UITextField *textfield=(UITextField*)[[searchBar subviews] objectAtIndex:1];
any idea how to fix this? thanks
// search bar
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0, 0.0, 190.0, 44.0)];
searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
UIView *searchBarView = [[UIView alloc] initWithFrame:CGRectMake(90.0, 0.0, 230.0, 44.0)];
searchBarView.autoresizingMask = 0;
searchBar.delegate = self;
searchBar.layer.borderColor=[UIColor whiteColor].CGColor;
UITextField *textfield=(UITextField*)[[searchBar subviews] objectAtIndex:1];
[searchBarView addSubview:searchBar];
self.navigationItem.titleView = searchBarView;
try this , it's work in Both IOS6 and IOS7+ and safe approch
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setFont:[UIFont fontWithName:#"ArialMT" size:10]];
It's not a good idea to assume that second searchBar's subview will be UITextField.
I printed out subviews for UISearchBar, that's what I got on iOS 7:
<__NSArrayM 0x17d141f0>(
<UIView: 0x17d34f40; frame = (0 0; 320 44); clipsToBounds = YES; autoresize = W+H; layer = <CALayer: 0x17d34fa0>>
)
Only one subview, so your ... objectAtIndex:1] will definitely crash.
You can use the following category for UIView to find UITextField in your searchBar:
#interface UIView(Utils)
-(UIView*)findSubviewRecursivelyOfClass:(Class)subviewClass;
#end
#implementation UIView(Utils)
-(UIView*)findSubviewRecursivelyOfClass:(Class)subviewClass
{
if( [self isKindOfClass:subviewClass] ) {
return self;
} else {
for( UIView* child in self.subviews ) {
UIView* result = [child findSubviewRecursivelyOfClass:subviewClass];
if( result ) {
return result;
}
}
return nil;
}
}
#end
Try this one for iOS7.
TESTED
for (id object in [searchBar subviews])
{
for (id subObject in [object subviews])
{
if ([subObject isKindOfClass:[UITextField class]])
{
UITextField *textfield=(UITextField*)subObject;
}
}
}
iOS6
for (id object in [searchBar subviews]) {
if ([object isKindOfClass:[UITextField class]]) {
UITextField *textfield=(UITextField*)object;
}
}
How about a recursive method that can trick to work in any version
UITextField *searchBarTextField = [self findTextFieldFromControl:self.placeSearchBar];
- (UITextField *) findTextFieldFromControl:(UIView *) view
{
for (UIView *subview in view.subviews)
{
if ([subview isKindOfClass:[UITextField class]])
{
return (UITextField *)subview;
}
else if ([subview.subviews count] > 0)
{
return [self findTextFieldFromControl:subview];
}
}
return nil;
}
I would like to customize the delete button which is shown when performing the 'swipe to left'-action on a tableview cell. I currently set up a subclass of a UITableViewCell but also want to customize the delete-button which is being shown.
My goal is to place three buttons when swiping.
I choose for another implementation where I was using a UIScrollview in each cell.
http://www.teehanlax.com/blog/reproducing-the-ios-7-mail-apps-interface/
Accepted answer will not work on iOS 7, as there is now UITableViewCellContentView in between. So subviews loop now should look like this(if you want to support older iOS versions too, use currently accepted answer for iOS 6.1-)
for (UIView *subview in self.subviews) {
for (UIView *subview2 in subview.subviews) {
if ([NSStringFromClass([subview2 class]) rangeOfString:#"Delete"].location != NSNotFound) {
// Do whatever you want here
}
}
}
This might help you.
- (void)willTransitionToState:(UITableViewCellStateMask)state
{
[super willTransitionToState:state];
if ((state & UITableViewCellStateShowingDeleteConfirmationMask) == UITableViewCellStateShowingDeleteConfirmationMask)
{
for (UIView *subview in self.subviews)
{
if ([NSStringFromClass([subview class]) isEqualToString:#"UITableViewCellDeleteConfirmationControl"])
{
UIImageView *deleteBtn = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 64, 33)];
[deleteBtn setImage:[UIImage imageNamed:#"arrow_left_s11.png"]];
[[subview.subviews objectAtIndex:0] addSubview:deleteBtn];
}
}
}
}
Referenced from:
Customize the delete button in UITableView
create custom delete button for uitableview
Custom Delete button On Editing in UITableView Cell
-(void)willTransitionToState:(UITableViewCellStateMask)state{
NSLog(#"EventTableCell willTransitionToState");
[super willTransitionToState:state];
if((state & UITableViewCellStateShowingDeleteConfirmationMask) == UITableViewCellStateShowingDeleteConfirmationMask)
{
UIImageView *deleteBtn = [[UIImageView alloc]initWithFrame:CGRectMake( 320,0, 228, 66)];
[deleteBtn setImage:[UIImage imageNamed:#"BtnDeleteRow.png"]];
[[self.subviews objectAtIndex:0] addSubview:deleteBtn];
[self recurseAndReplaceSubViewIfDeleteConfirmationControl:self.subviews];
[self performSelector:#selector(recurseAndReplaceSubViewIfDeleteConfirmationControl:) withObject:self.subviews afterDelay:0];
}
}
The solutions above didn't work for me for iOS 7, at - (void)willTransitionToState:, the delete button wasn't in the view heirarchy so I wasn't able to manipulate anything. I ended up doing everything on - (void)didTransitionToState:. The example below was specifically for when my cells had some spacing at the top so I'm altering the frame of the delete button. If you want to customize the delete button, you can just add a view on top of the delete button or replace it with your own UIButton
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
//your own stuff
//for some reason, editingAccessoryView cannot be nil and must have a non-CGRectZero frame
self.editingAccessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
}
return self;
}
- (void)didTransitionToState:(UITableViewCellStateMask)state
{
[super didTransitionToState:state];
if ((state & UITableViewCellStateShowingDeleteConfirmationMask) == UITableViewCellStateShowingDeleteConfirmationMask)
{
UIView *deleteButton = [self deleteButtonSubview:self];
if (deleteButton) {
CGRect frame = deleteButton.frame;
frame.origin.y += defined_padding;
frame.size.height -= defined_padding;
deleteButton.frame = frame;
}
}
}
- (UIView *)deleteButtonSubview:(UIView *)view
{
if ([NSStringFromClass([view class]) rangeOfString:#"Delete"].location != NSNotFound) {
return view;
}
for (UIView *subview in view.subviews) {
UIView *deleteButton = [self deleteButtonSubview:subview];
if (deleteButton) {
return deleteButton;
}
}
return nil;
}
I'm trying to apply a custom font throughout my iOS app. I found that I could use:
[[UILabel appearance] setFont:[UIFont fontWithName:#"Proxima Nova" size:17.0]];
To set the default font and size for all UILabels. However, not all my UILabels share the same font size.
In Set a default font for whole iOS app?, someone had the same concern, and was told to set the size parameter to 0.0 to only set the font and not font size.
When I tried doing this, all the UILabel text in my app disappeared (because evidently iOS took the 0.0 font size literally).
Any suggestions as to how I can universally set a font but not size? Thanks a lot!
- (void)viewDidLoad
{
[super viewDidLoad];
[self setFontFamily:#"FagoOfficeSans-Regular" forView:self.view andSubViews:YES];
}
-(void)setFontFamily:(NSString*)fontFamily forView:(UIView*)view andSubViews:(BOOL)isSubViews
{
if ([view isKindOfClass:[UILabel class]])
{
UILabel *lbl = (UILabel *)view;
[lbl setFont:[UIFont fontWithName:fontFamily size:[[lbl font] pointSize]]];
}
if (isSubViews)
{
for (UIView *sview in view.subviews)
{
[self setFontFamily:fontFamily forView:sview andSubViews:YES];
}
}
}
Here is a solution in Objective-C, put this category anywhere you want to change UILabel Apperance without setting UILabel FontSize:
#implementation UILabel (SubstituteFontName)
- (void)setSubstituteFontName:(NSString *)name UI_APPEARANCE_SELECTOR {
self.font = [UIFont fontWithName:name size:self.font.pointSize];
}
#end
Then, you can change the Apperance with:
[[UILabel appearance] setSubstituteFontName:#"SourceSansPro-Light"];
I've used the accepted answer in my project, but needed a more generic function, so it'll change the font to every one possible, also I've chose to set a mapping between some stock fonts to our custom fonts, so they'll be accessible via storybuilder and xib files as well.
+ (void)setupFontsForView:(UIView *)view andSubViews:(BOOL)isSubViews
{
if ([view respondsToSelector:#selector(setFont:)] && [view respondsToSelector:#selector(font)]) {
id viewObj = view;
UIFont *font = [viewObj font];
if ([font.fontName isEqualToString:#"AcademyEngravedLetPlain"]) {
[viewObj setFont:[UIFont fontWithName:PRIMARY_FONT size:font.pointSize]];
} else if ([font.fontName hasPrefix:#"AmericanTypewriter"]) {
[viewObj setFont:[UIFont fontWithName:SECONDARY_FONT size:font.pointSize]];
}
}
if (isSubViews) {
for (UIView *sview in view.subviews) {
[self setupFontsForView:sview andSubViews:YES];
}
}
}
By writing the category for the Label we can change the fonts of entire app.
#implementation UILabel (CustomeFont)
-(void)awakeFromNib
{
[super awakeFromNib];
[self setBackgroundColor:[UIColor clearColor]];
[self setFont:[UIFont fontWithName:#"Helvetica" size:self.font.pointSize]];
}
#end
Swift3
https://gist.github.com/dimohamdy/c47992e14c1a3ee3315c06e2480e121e
you can't set appearance for label font that make me create other value of default font and when i set the new font i get only the name of new font and old size of label and create other font and set this font
//when you want to set font for all labels in Application
UILabel.appearance().defaultFont = UIFont.systemFont(ofSize: 15/*Any Value*/, weight: UIFontWeightThin)
extension UILabel{
dynamic var defaultFont: UIFont? {
get { return self.font }
set {
//get old size of lable font
let sizeOfOldFont = self.font.pointSize
//get new name of font
let fontNameOfNewFont = newValue?.fontName
self.font = UIFont(name: fontNameOfNewFont!, size: sizeOfOldFont)
}
}
}
UIView+DefaultFontAndColor.h
#import <UIKit/UIKit.h>
#interface UIView (DefaultFontAndColor)
-(void)setDefaultFontFamily:(NSString*)fontFamily andSubViews:(BOOL)isSubViews andColor:(UIColor*) color;
#end
UIView+DefaultFontAndColor.m
#import "UIView+DefaultFontAndColor.h"
#implementation UIView (DefaultFontAndColor)
//sets the default font for view classes by default
-(void)setDefaultFontFamily:(NSString*)fontFamily andSubViews:(BOOL)isSubViews andColor: (UIColor*) color
{
if ([self isKindOfClass:[UILabel class]])
{
UILabel *lbl = (UILabel *)self;
[lbl setFont:[UIFont fontWithName:fontFamily size:[[lbl font] pointSize]]];
if( color )
lbl.textColor = color;
}
else if ([self isKindOfClass:[UIButton class]])
{
UIButton *btn = (UIButton *)self;
[btn.titleLabel setFont:[UIFont fontWithName:fontFamily size:[[btn.titleLabel font] pointSize]]];
if( color )
{
btn.tintColor = color;
}
}
if (isSubViews)
{
for (UIView *sview in self.subviews)
{
[sview setDefaultFontFamily:fontFamily andSubViews:YES andColor:color];
}
}
}
#end
#usage: without color:
#import "UIView+DefaultFontAndColor.h"
UIView myView = [[UIView alloc] init]
[myView setDefaultFontFamily:#"Arial" andSubViews:YES andColor:nil];
#usage: with color:
#import "UIView+DefaultFontAndColor.h"
UIView myView = [[UIView alloc] init]
[myView setDefaultFontFamily:#"Arial" andSubViews:YES andColor:[UIColor greenColor] ];
Raegtime's answer ala Swift...
import UIKit
extension UIViewController {
func setFontFamilyForView(_ fontFamily: String, view: UIView, andSubviews: Bool) {
if let label = view as? UILabel {
label.font = UIFont(name: fontFamily, size: label.font.pointSize)
}
if let textView = view as? UITextView {
textView.font = UIFont(name: fontFamily, size: textView.font!.pointSize)
}
if let textField = view as? UITextField {
textField.font = UIFont(name: fontFamily, size: textField.font!.pointSize)
}
if andSubviews {
for v in view.subviews {
setFontFamilyForView(fontFamily, view: v, andSubviews: true)
}
}
}
}
In my application I have to add a search bar at the head of the tableview. I am able to add the searchbar but problem is without adding default search bar of ios can i add my customize search bar?? I am giving an image to see what types of search bar will be there...
you can subclass the UISearchBar and override the layoutSubviews method :
- (void)layoutSubviews {
UITextField *searchField;
NSUInteger numViews = [self.subviews count];
for(int i = 0; i < numViews; i++) {
if([[self.subviews objectAtIndex:i] isKindOfClass:[UITextField class]]) { //conform?
searchField = [self.subviews objectAtIndex:i];
}
}
if(!(searchField == nil)) {
searchField.textColor = [UIColor whiteColor];
[searchField setBackground: [UIImage imageNamed:#"yourImage.png"] ];
[searchField setBorderStyle:UITextBorderStyleNone];
}
[super layoutSubviews];
}
Also you can :
//to clear searchbar backgraound
- (void) clearSearchBarBg
{
for (UIView *subview in theSearchBar.subviews)
{
if ([subview isKindOfClass:NSClassFromString(#"UISearchBarBackground")])
{
[subview removeFromSuperview];
break;
}
}
}
//display showSearchButtonInitially in a keyboard
- (void)showSearchButtonInitially
{
UIView * subview;
NSArray * subviews = [theSearchBar subviews];
for(subview in subviews)
{
if( [subview isKindOfClass:[UITextField class]] )
{
NSLog(#"setEnablesReturnKeyAutomatically");
[((UITextField*)subview) setEnablesReturnKeyAutomatically:NO];
((UITextField*)subview).delegate=self;
[((UITextField*)subview) setEnabled:TRUE];
((UITextField*)subview).borderStyle = UITextBorderStyleNone;
break;
}
}
}
Look for Apple DOC for UISearchBar
You have bunch of methods there to get whatever you want
You can get UITextView Inside the search bar by
UITextField *textField = [searchBar.subviews objectAtIndex:2];
if ([textField isKindOfClass:[UITextField class]]) {
//Do your customization
}
Again look for AppleDoc for UITextField. You have bunch of methods for that also.
Yeah definitely. You can make your custom search bar (which is a sub-class of UIView) and add it as subview to the tableHeaderView.
[[searchBarDesign.subviews objectAtIndex:0] removeFromSuperview];
here searchBarDesign is my searchBar name.
I think it's better just set all properties of UISearchBar when it is loaded.
#interface MySearchBar : UISearchBar
#end
#implementation MySearchBar
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self myInitialize];
}
return self;
}
-(void)awakeFromNib
{
[super awakeFromNib];
[self myInitialize];
}
-(void)myInitialize
{
self.backgroundImage = [UIImage imageNamed:#"image.png"];
for (UIView* subview in self.subviews) {
if ([subview isKindOfClass:[UITextField class]]) {
//customize text field
UITextField* textfield = (UITextField*) subview;
}
}
}
#end
Okay, a short breif of my app before i explain the problem.
My app has two views for it's custom TableViewCell, one frontview, and one backview(which is revealed once you swipe your finger across the cell, much like the twitter-app).
Anywho, i wanted to have some buttons on the backview. I did this in the cellForRowAtIndexPath-method
The first you'll see here is how i assign labels to the cells. The secondary thing you'll see is the button. It's working fine.
UILabel *nextArtist = [UILabel alloc];
nextArtist.text = #"Rihanna";
nextArtist.tag = 4;
[cell setNextArtist:nextArtist];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(6 ,31, 110, 20);
[button setImage:[UIImage imageNamed:#"radionorge.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(touched:) forControlEvents:UIControlEventTouchUpInside];
[cell.backView addSubview:button];
But, it's in the next method the problem occours.
-(void)touched:(id)sender {
// Here i want to get the UILabels for each cell. Such as nextArtist.
if ([sender isKindOfClass:[UIButton class]]) {
UIButton *button = (UIButton *)sender;
UIView *contentView = button.superview;
UIView *viewWithTag4 = [contentView viewWithTag:4];
if ([viewWithTag1 isKindOfClass:[UILabel class]]) {
UILabel *titleLabel = (UILabel *)viewWithTag4;
NSLog(#"Label: ",titleLabel.text);
}
}
}
So, i realised that i cannot just go to the superview of the button and find my labels there, because their in another subview. I have scanned all my views, but still cannot find the label.
I am very new at this, and the subclassing of the TableView-cell is something i implemented from someone who posted their code.
But, my assumption is that there are noe UILabels in my view, because i am not adding them as views, only drawing them, with the drawTextInRect-function.
[nextArtist drawTextInRect:CGRectMake(boundsX+200 ,46, 110, 15)];
I have tried to adding them as subviews, but with no luck. Could anyone help me?
// Some more code you may need to solve the puzzle(this is where the cell-views are made)
#implementation RadioTableCellView
- (void)drawRect:(CGRect)rect {
if (!self.hidden){
[(RadioTableCell *)[self superview] drawContentView:rect];
}
else
{
[super drawRect:rect];
}
}
#end
#implementation RadioTableCellBackView
- (void)drawRect:(CGRect)rect {
if (!self.hidden){
[(RadioTableCell *)[self superview] drawBackView:rect];
}
else
{
[super drawRect:rect];
}
}
#end
#interface RadioTableCell (Private)
- (CAAnimationGroup *)bounceAnimationWithHideDuration:(CGFloat)hideDuration initialXOrigin:(CGFloat)originalX;
#end
#implementation RadioTableCell
#synthesize contentView;
#synthesize backView;
#synthesize contentViewMoving;
#synthesize selected;
#synthesize shouldSupportSwiping;
#synthesize shouldBounce;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
[self setBackgroundColor:[UIColor clearColor]];
RadioTableCellView * aView = [[RadioTableCellView alloc] initWithFrame:CGRectZero];
[aView setClipsToBounds:YES];
[aView setOpaque:YES];
[aView setBackgroundColor:[UIColor clearColor]];
[self setContentView:aView];
[aView release];
RadioTableCellBackView * anotherView = [[RadioTableCellBackView alloc] initWithFrame:CGRectZero];
[anotherView setOpaque:YES];
[anotherView setClipsToBounds:YES];
[anotherView setHidden:YES];
[anotherView setBackgroundColor:[UIColor clearColor]];
[self setBackView:anotherView];
[anotherView release];
// Backview must be added first!
// DO NOT USE sendSubviewToBack:
[self addSubview:backView];
[self addSubview:contentView];
[self setContentViewMoving:NO];
[self setSelected:NO];
[self setShouldSupportSwiping:YES];
[self setShouldBounce:YES];
[self hideBackView];
}
return self;
}
Please help me, or at least point me in a direction or two!
//////////////////////////
Updated with some new code:
This is inside my RadioCustomCell, a UIView-subclass. It's here the UILabels are drawn
#import "RadioCustomCell.h"
#implementation RadioCustomCell
#synthesize nowTitle,nowArtist,nextTitle,nextArtist,ChannelImage;
// Setting the variables
- (void)setNowTitle:(UILabel *)aLabel {
if (aLabel != nowTitle){
[nowTitle release];
nowTitle = [aLabel retain];
[self setNeedsDisplay];
}
}
- (void)setNowArtist:(UILabel *)aLabel {
if (aLabel != nowArtist){
[nowArtist release];
nowArtist = [aLabel retain];
[self setNeedsDisplay];
}
}
- (void)setNextTitle:(UILabel *)aLabel {
if (aLabel != nextTitle){
[nextTitle release];
nextTitle = [aLabel retain];
[self setNeedsDisplay];
}
}
- (void)setNextArtist:(UILabel *)aLabel {
if (aLabel != nextArtist){
[nextArtist release];
nextArtist = [aLabel retain];
[self setNeedsDisplay];
}
}
- (void)setChannelImage:(UIImage *)aImage {
if (aImage != ChannelImage){
[ChannelImage release];
ChannelImage = [aImage retain];
[self setNeedsDisplay];
}
}
- (void)drawContentView:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
//UIColor * backgroundColour = [UIColor whiteColor];
UIColor *backgroundColour = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"CellBackground.png"]];
[backgroundColour set];
CGContextFillRect(context, rect);
CGRect contentRect = self.contentView.bounds;
CGFloat boundsX = contentRect.origin.x;
[ChannelImage drawInRect:CGRectMake(boundsX+120 ,25, 75, 35)];
nowTitle.enabled = YES;
nowTitle.textAlignment = UITextAlignmentCenter;
nowTitle.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size: 14.0];
nowTitle.textColor = [UIColor blackColor];
nowTitle.backgroundColor = [UIColor clearColor];
//[nowTitle drawTextInRect:CGRectMake(boundsX+6 ,31, 110, 20)];
// Trying to add a subview instead of drawing the text
nowTitle.frame = CGRectMake(boundsX+6 ,31, 110, 20);
[self addSubview:nowTitle];
// I have also tried adding it to super, no effect.
nowArtist.enabled = YES;
nowArtist.textAlignment = UITextAlignmentCenter;
nowArtist.font = [UIFont fontWithName:#"HelveticaNeue" size: 10.0];
nowArtist.textColor = [UIColor blackColor];
nowArtist.backgroundColor = [UIColor clearColor];
[nowArtist drawTextInRect:CGRectMake(boundsX+6 ,46, 110, 15)];
nextTitle.enabled = NO;
nextTitle.textAlignment = UITextAlignmentCenter;
nextTitle.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size: 12.0];
[nextTitle drawTextInRect:CGRectMake(boundsX+200 ,31, 110, 20)];
nextArtist.enabled = NO;
nextArtist.textAlignment = UITextAlignmentCenter;
nextArtist.font = [UIFont fontWithName:#"HelveticaNeue" size: 9.0];
[nextArtist drawTextInRect:CGRectMake(boundsX+200 ,46, 110, 15)];
}
You just forgot to init your UILabel in your very first line of code. :)
At the first look, I can be sure that if you draw the text (and the text is not the label), then there is no UILabel at all in your cell. You can ignore that way.
So, if you don't have the UILabel, how you can get the text. Because your contentView is indeed a RadioTableCellView, then it is not hard. In your class, just public a property called nextArtist. When your button is tapped, look up for the contentView (by calling some superview), cast it to RadioTableCellView then get the nextArtist out
Without writing a bunch of code, here's my best guess.
You're going to need to assign tags to aView and anotherView in your initWithStyle: method. I'm going to assume you have a couple constants: BACK_VIEW_TAG and FRONT_VIEW_TAG.
In your touched: method, walk up the view hierarchy until you find your UITableViewCell.
UIView *currentView = button.superView;
while (![currentView isKindOfClass:UITableViewCell] && currentView != nil) {
currentView = currentView.parent;
}
Get the front view and back view (or which ever you want) from the table cell's contentView using the tags.
if (currentView != nil) {
UITableViewCell *cellView = (UITableViewCell *)currentView)
UIView *cellContentView = cellView.contentView;
UIView *backView = [cellContentView viewWithTag:BACK_VIEW_TAG];
UIView *frontView = [cellContentView viewWithTag:FRONT_VIEW_TAG];
// Get other views from frontView and backView using viewWithTag:.
}
Note that you should be adding views to your UITableViewCell subclass's contentView, not to the UITableViewCell directly. See Programmatically Adding Subviews to a Cell’s Content View for details.