lunes, 30 de mayo de 2016

iOS: UITableView selection (row highlight)

// Multiple selections
self.tableView.allowsMultipleSelection = YES;

// Transparent "highlight" list
self.tableView.allowsSelection = NO;

UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // ...
}

lunes, 23 de mayo de 2016

iOS: Tex with various colors or formats

Objective C

- (NSAttributedString*)_text
{
    NSString* differentColorText = @"differentColorText";
    
    NSString* fullText = [NSString stringWithFormat:@"some text %@ some more text"differentColorText];
    
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc]initWithString:fullText];
    
    NSRange range = [fullText rangeOfString: differentColorText];
    
    [string addAttribute:NSForegroundColorAttributeName
                   value:[UIColor redColor]
                   range:range];

    [string addAttribute:NSFontAttributeName 
                   value:[UIFont boldSystemFontOfSize:12      
                   range: range];
    
    return string;


}

[self.label setAttributedText:[self _text]];

Swift


let text = "some text"
let fullText = "something else \(text)"
let range = (fullText as NSString).range(of: text)
        
let attributedText = NSMutableAttributedString(string: fullText)
attributedText.addAttribute(NSAttributedStringKey.font, value: UIFont.boldSystemFont(ofSize: 17), range: range)
        

field.attributedText = attributedText

viernes, 20 de mayo de 2016

iOS: UITextField with image on its right

Interface

IB_DESIGNABLE

@interface MyDropDownTextField : MyTextField

@property (nonatomic, strong) IBInspectable UIImage* rightViewImage;


@end

Implementation

#import "MyDropDownTextField.h"

@implementation MyDropDownTextField

- (void)awakeFromNib
{
    [super awakeFromNib];
    
    self.backgroundColor = [UIColor clearColor];
    self.rightViewMode = UITextFieldViewModeAlways;
    self.rightView = [[UIImageView alloc] initWithImage:self.rightViewImage];

    CGFloat height = self.frame.size.height;
    CGFloat y = (height - 6)/2;
    self.rightView.frame = CGRectMake(self.rightView.frame.origin.x, y, 9, 6);
}

- (CGRect) rightViewRectForBounds:(CGRect)bounds {
    
    CGRect textRect = [super rightViewRectForBounds:bounds];
    textRect.origin.x -= 10;
    return textRect;
}

@end

jueves, 19 de mayo de 2016

iOS: UIScrollView remove space saved for NavigationBar


self.automaticallyAdjustsScrollViewInsets = NO;

iOS: UITableView remove separator line

Objective C

self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

Swift


tableView.separatorStyle = .none

lunes, 2 de mayo de 2016

iOS: Constraint animation

I wanted to animate a floating bar.

- (void)viewDidLoad
{
[super viewDidLoad];

self.floatingBar.hidden = YES;
    for (NSLayoutConstraint* contraint in self.view.constraints) {
        if ([contraint.identifier isEqualToString:@"top"]) {
            contraint.constant = -65 - self.floatingBar.frame.size.height;
            break;
        }

    }

...
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGPoint offset = scrollView.contentOffset;
    
    if (offset.y < 400*) {
        if (!self.floatingBar.hidden) {
            self.floatingBar.hidden = YES;
            for (NSLayoutConstraint* contraint in self.view.constraints) {
                if ([contraint.identifier isEqualToString:@"top"])** {
                    contraint.constant = -65*** - self.floatingBar.frame.size.height;
                    break;
                }
            }
        }
        
    } else if (offset.y > 400) {
        if (self.floatingBar.hidden) {
            self.floatingBar.hidden = NO;
            
            [self.view layoutIfNeeded];
            
            [UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
                
                for (NSLayoutConstraint* contraint in self.view.constraints) {
                    if ([contraint.identifier isEqualToString:@"top"]) {
                        contraint.constant = -65;
                        break;
                    }
                }
                
                [self.view layoutIfNeeded];
                
            } completion:^(BOOL finished) {
                
            }];
        }
    }
}

* 400 was my threshhold
** Name of my constraint (identifier I used in interface builder)
*** I wanted my bar to be at the top of the window (under status bar and navigation bar)