페이지

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.



2017년 2월 18일 토요일

Swift Inheritance, override, final

//Inheritance, override, final 


//Inheritance, override, final 

class Vehicle{
    var totalVehicles : Int = 0
    var desc:String {
        return "totalVehicles : \(totalVehicles)."
    }
}

class Bycle: Vehicle {
    override init(){
        super.init()
        self.totalVehicles = 2
    }
    override var desc : String {
        return super.desc + ", in bycle"
    }
    final func noOverrideMethod(){
        print("final keyword mean this cannot be allowed to override any more")
    }
}
class Bycle4Wheels : Bycle {
    override init(){
        super.init()
        self.totalVehicles = 4
    }

    override var desc : String {
        return super.desc + " with 4 wheels "
    }
}
var b = Bycle()
print( b.desc)
b.noOverrideMethod()
var f = Bycle4Wheels()
print( f.desc)

Swift Instance Method, Type Method,subscript

struct StructSample{
    static func typeMethod(){
        print("i am type method")
    }
    func instanceMethod(){
        print("i am instance method")
    }
}

var s = StructSample()
s.instanceMethod()
StructSample.typeMethod()

class ClassSample{
    //class keyword allow subclasses to override superclass implements
    class func typeMethod(){
        print("i am type method")
    }

    //static keyword not allow subclasses to override superclass implements 
    static func staticTypeMethod(){
        print(" static method from statcTypeMechod")
    }

    func instanceMethod(){
        print("i am instance method")
    }
}
var c = ClassSample()
c.instanceMethod()
ClassSample.typeMethod()


//static, class modifiers ?
class SomeClass {
    class var overrideType: Int {
        get {
            return 107
        }
    }
    static var type2 : Int  {
        get {
            return 1
        }
    }
}

print(SomeClass.overrideType)
print(SomeClass.type2)

//subscript
struct SubscriptSample{
    var weight : Int
    subscript(index:Int) -> Int {
        return index * weight
    }
}

var s = SubscriptSample(weight:3)
print( s[2],s[3])