페이지

2017년 3월 30일 목요일

iOS get UIColor from HexCode String


+ (UIColor *)colorWithHexCode:(NSString *)hexCode {
    NSString *arrangedCode = [hexCode stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    arrangedCode = arrangedCode.uppercaseString;
    if (arrangedCode.length < 6) {
        return [UIColor blackColor];
    }
    NSArray *deletingCharacters = @[@"0X", @"#"];
    for (NSString *deletingCharacter in deletingCharacters) {
        if ([arrangedCode hasPrefix:deletingCharacter]) {
            arrangedCode = [arrangedCode substringFromIndex:deletingCharacter.length];
            break;
        }
    }
    if (arrangedCode.length != 6) {
        return [UIColor blackColor];
    }
    NSString *redCode = [arrangedCode substringWithRange:NSMakeRange(0, 2)];
    NSString *greenCode = [arrangedCode substringWithRange:NSMakeRange(2, 2)];
    NSString *blueCode = [arrangedCode substringWithRange:NSMakeRange(4, 2)];
    unsigned int red, green, blue = 0;
    [[NSScanner scannerWithString:redCode] scanHexInt:&red];
    [[NSScanner scannerWithString:greenCode] scanHexInt:&green];
    [[NSScanner scannerWithString:blueCode] scanHexInt:&blue];
    return [UIColor colorWithRed:(red / 255.0f)
                           green:(green / 255.0f)
                            blue:(blue / 255.0f)
                           alpha:1.0f];
}

iOS UILabel attributedText NSForegroundColorAttributeName property doesn't work. its forecolor is transparent.






@interface UIStrokeLabel : UILabel
@property (nonatomic, assign) CGFloat stokeWidth;
@end



@implementation UIStrokeLabel
- (void)drawTextInRect:(CGRect)rect{
    self.attributedText = [[NSAttributedString alloc]
                           initWithString:self.text
                           attributes:@{
                                        NSStrokeWidthAttributeName: [NSNumber numberWithFloat:self.strokeWidth],
                                        NSStrokeColorAttributeName:[UIColor blueColor],
                                        NSForegroundColorAttributeName:self.textColor
                                        }
                           ];
    
    [super drawTextInRect:rect];
}
@end

self.strokeWidth must be under 0. 
if it is over 0, text fore color is transparent.

iOS update multiplier value of NSLayoutConstraint

- (instancetype) updateMultiplier:(CGFloat)multiplier {

    [NSLayoutConstraint deactivateConstraints:[NSArray arrayWithObjects:self, nil]];
    NSLayoutConstraint *newConstraint = [NSLayoutConstraint constraintWithItem:self.firstItem
                                                                     attribute:self.firstAttribute
                                                                     relatedBy:self.relation
                                                                        toItem:self.secondItem
                                                                     attribute:self.secondAttribute
                                                                    multiplier:multiplier
                                                                      constant:self.constant];

    [newConstraint setPriority:self.priority];
    newConstraint.shouldBeArchived = self.shouldBeArchived;
    newConstraint.identifier = self.identifier;
    newConstraint.active = true;


    [NSLayoutConstraint activateConstraints:[NSArray arrayWithObjects:newConstraint, nil]];
    return newConstraint;

}

/// Usage


@property(nonatomic, strong) NSLayoutConstraint contraintCenterX;

self.constraintCenterX = [self.constraintCeterX updateMultiplier:0.6];


autolayout not work in viewWillAppear but work in viewDidAppear.

autolayout only works after viewDidAppear.
so if you want it to do in viewWillAppear, call layoutIfNeeded there.



ex)



- (void) viewWillAppear {

....

    [myView layoutIfNeeded];

    // myView autolayouted!!

}

iOS Creating Custom View

Creating MyView

1. Creating MyView Class

@interface MyView : UIView

@property(nonatomic,strong) UIView* contentView;

@property(nonatomic,strong) NSBundle* bundle;

@end





#import “MyView.h”

@implementation MyView

- (instancetype)initWithCoder:(NSCoder *)aDecoder{

    self = [super initWithCoder:aDecoder];

    if( self ){

        self.bundle = [NSBundle bundleForClass:[self class]];

        if (self.subviews.count == 0) {

            self.contentView = [[self.bundle loadNibNamed:NSStringFromClass([self class]) owner:self options:nil] objectAtIndex:0];

            [self addSubview: self.contentView];

            self.contentView.frame = self.bounds;

        }

    }

    return self;

}



@end



2. Creating MyView.nib

place "MyView" in the Class field of the File's Owner.