Рейтинг
0.00
голосов:
0
avatar

Tips & Tricks  

Программное добавление текста на UIImage

Для этого используйте следующую функцию:

-(UIImage *)addText:(UIImage *)img text:(NSString *)text1{
    int w = img.size.width;
    int h = img.size.height; 
    //lon = h - lon;
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
    CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);
    CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1);
    char* text = (char *)[text1 cStringUsingEncoding:NSASCIIStringEncoding];
    CGContextSelectFont(context, "Arial", 18, kCGEncodingMacRoman);
    CGContextSetTextDrawingMode(context, kCGTextFill);
    CGContextSetRGBFillColor(context, 255, 255, 255, 1);

    //rotate text
    CGContextSetTextMatrix(context, CGAffineTransformMakeRotation( -M_PI/4 ));
    CGContextShowTextAtPoint(context, 4, 52, text, strlen(text));
    CGImageRef imageMasked = CGBitmapContextCreateImage(context);
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);
    return [UIImage imageWithCGImage:imageMasked];
}

Выравнивание Labels в UITableViewCell

В методе

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

переопределяем высоту UITableViewCell.

В методе
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

инициализируем cell с стилем UITableViewCellStyleSubtitle:

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];

Текст descriptionTextLabel задаем больше одной строки. В таком случае текст labels центрируется и выходит за рамки cell.

( Читать дальше )

Динамический UILabel

Если вы не знаете сколько строчек займет текст при выводе в UILabel, необходимо добавить такой код:

myUILabel.numberOfLines = 0;

В таком случае количество строчек будет подобрано автоматически.

Вычисляем высоту UILabel в зависимости от текста

Для определения высоты нужно воспользоваться следующей функцией:

+(float) calculateHeightOfTextFromWidth:(NSString*) text: (UIFont*)withFont: (float)width :(UILineBreakMode)lineBreakMode
{
    [text retain];
    [withFont retain];
    CGSize suggestedSize = [text sizeWithFont:withFont constrainedToSize:CGSizeMake(width, FLT_MAX) lineBreakMode:lineBreakMode];
 
    [text release];
    [withFont release];
    return suggestedSize.height;
}

Изменяем User Agent в iPhone

Как известно, User Agent необходим для «идентификации» пользователя, а конкретнее, для определения, каким клиентом или через какое устройство заходит пользователь к вам на сайт. В зависимости от этого, можно «подставлять» необходимый контент (данные, разметку). Это актуально в последнее время по причине большого роста мобильных устройств.

( Читать дальше )