'ios'에 해당하는 글 4건


NSTextStorage *textStorage = [[NSTextStorage alloc] initWithAttributedString:labelText];
NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
[textStorage addLayoutManager:layoutManager];
CGRect bounds = label.bounds;
NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:bounds.size];
[layoutManager addTextContainer:textContainer];

Now you just need to find the index of the character that was clicked, which is simple!

NSUInteger characterIndex = [layoutManager characterIndexForPoint:location
                                                  inTextContainer:textContainer
                         fractionOfDistanceBetweenInsertionPoints:NULL];

Which makes it trivial to find the word itself:

if (characterIndex < textStorage.length) {
  [labelText.string enumerateSubstringsInRange:NSMakeRange(0, textStorage.length)
                                       options:NSStringEnumerationByWords
                                    usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
                                      if (NSLocationInRange(characterIndex, enclosingRange)) {
                                        // Do your thing with the word, at range 'enclosingRange'
                                        *stop = YES;
                                      }
                                    }];
}



다른 방법


1) Subclass UILabel, and add a touchesEnded: to recognize touches on your label.
2) Store in an array all the sizes of the letters using system method sizeWithFont:
3) Get the touch point and compare with the letters size.
Done!

Create an UILabel subclass
//
// APLabel.h
//
#import <uikit /UIKit.h>

@protocol APLabelDelegate <nsobject>
@required
  - (void) didLetterFound:(char)letter;
@end

@interface APLabel : UILabel

@property (nonatomic, assign) id <aplabeldelegate> delegate;

@end

//
// APLabel.m
//
#import "APLabel.h"

@implementation APLabel

- (id)initWithCoder:(NSCoder *)aDecoder
{
  if ( self = [super initWithCoder:aDecoder])
  {
    self.backgroundColor          = [UIColor greenColor];
    self.userInteractionEnabled   = YES;
  }
  return self;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
  UITouch *touch = [[touches allObjects] objectAtIndex:0];
  CGPoint pos    = [touch locationInView:self];

  int sizes[self.text.length];
  for ( int i=0; i<self .text.length; i++ )
  {
    char letter         = [self.text characterAtIndex:i];
    NSString *letterStr = [NSString stringWithFormat:@"%c", letter];
    
    CGSize letterSize   = [letterStr sizeWithFont:self.font];
    sizes[i]            = letterSize.width;
  }

  int sum = 0;
  for ( int i=0; i<self.text.length; i++)
  {
    sum += sizes[i];
    if ( sum >= pos.x )
    {
      [ _delegate didLetterFound:[ self.text characterAtIndex:i] ];
      return;
    }
  }
}
@end


'ios' 카테고리의 다른 글

json dictionary로 가져오기  (0) 2017.01.20
일정 시간 지연후 실행  (0) 2016.12.19
ios info.plist 권한수정  (0) 2016.12.01

WRITTEN BY
carbo

,

NSData *data = [response dataUsingEncoding:NSUTF8StringEncoding];

            dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

'ios' 카테고리의 다른 글

UILable 터치시 단어 가져오기  (0) 2017.06.30
일정 시간 지연후 실행  (0) 2016.12.19
ios info.plist 권한수정  (0) 2016.12.01

WRITTEN BY
carbo

,

일정 시간 지연후 실행

ios 2016. 12. 19. 10:22
-(IBAction)buttonPressed:(UIButton *) sender {
[self performSelector:@selector(addCoins) withObject:nil afterDelay:30];
}

-(void)addCoins {
//put whatever code you want to happen after the 30 seconds
}


'ios' 카테고리의 다른 글

UILable 터치시 단어 가져오기  (0) 2017.06.30
json dictionary로 가져오기  (0) 2017.01.20
ios info.plist 권한수정  (0) 2016.12.01

WRITTEN BY
carbo

,

ios info.plist 권한수정

ios 2016. 12. 1. 12:54

위 오류를 해결하려면 키와 사용목적을 Info.plist파일에 추가한다. 예로 사진 라이브러리에 접근을 하려면 NSPhotoLibraryUsageDescription키를 사용한다.


<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>NSPhotoLibraryUsageDescription</key>
    <string>사용목적을 여기에 작성</string>
    ...
</dict>
</plist>


  • 미디어 라이브러리 접근 : NSAppleMusicUsageDescription
  • 블루투스 인터페이스 접근 : NSBluetoothPeripherealUsageDescription
  • 달력 접근 : NSCalendarUsageDescription
  • 카메라 접근 : NSCameraUsageDescription
  • 연락처에 접근 : NSContactsUsageDescription
  • 헬스 데이터 접근 : NSHealthShareUsageDescription
  • 건강 데이터 접근 : NSHealthUpdateUSageDescription
  • HomeKit 설정 데이터 접근 : NSHomeKitUsageDescription
  • 위치정보 접근 (항상 허용) :  NSLocationAlwaysUsageDescription
  • 위치정보 접근 (사용할 경우만) : NSLocationWhenInUseUsageDescription
  • 마이크 접근 : NSMicrophoneUsageDescription
  • 사진 라이브러리 접근 : NSPhotoLibraryUsageDescription


'ios' 카테고리의 다른 글

UILable 터치시 단어 가져오기  (0) 2017.06.30
json dictionary로 가져오기  (0) 2017.01.20
일정 시간 지연후 실행  (0) 2016.12.19

WRITTEN BY
carbo

,